Excel 增加数据点之间的时间差
作为自动数据处理的一部分,我正在尝试制作一个宏来增加数据点之间的时间,但它目前花费的时间太长了 我的一个传感器每10秒记录一个数据点,我想把这个dt增加到1小时。为此,我编写了一些非常简单(低效)的代码(见下文),这些代码确实有效,但处理一周的数据需要10-40分钟,这远远不够理想 我看到过关于使用数组的半相似问题的建议,但是我对此没有经验,不知道它是否适用于此目标Excel 增加数据点之间的时间差,excel,vba,Excel,Vba,作为自动数据处理的一部分,我正在尝试制作一个宏来增加数据点之间的时间,但它目前花费的时间太长了 我的一个传感器每10秒记录一个数据点,我想把这个dt增加到1小时。为此,我编写了一些非常简单(低效)的代码(见下文),这些代码确实有效,但处理一周的数据需要10-40分钟,这远远不够理想 我看到过关于使用数组的半相似问题的建议,但是我对此没有经验,不知道它是否适用于此目标 Do While Cells(row + 1, 2).Value <> "" If Cells
Do While Cells(row + 1, 2).Value <> ""
If Cells(row + 1, 2).Value - Cells(row, 2).Value < 1 / 24.05 Then
Rows(row + 1).Select
Selection.Delete Shift:=xlUp
Else
row = row + 1
End If
Loop
这将对您的执行时间有很大帮助:
Sub Change_dt()
Dim target As Single
target = Sheets("Controller").Cells(16, 9).Value
Dim arrSource As Variant
With ThisWorkbook.Sheets("Raw data")
arrSource = .UsedRange.Value 'this will input the whole used sheet inside the array
Dim finalArr As Variant
ReDim finalArr(1 To UBound(arrSource), 1 To UBound(arrSource, 2))
.Cells.Delete 'will clean the worksheet
Dim i As Long, x As Long, j As Long
x = 1
For i = 5 To UBound(arrSource)
On Error Resume Next
If arrSource(i + 1, 2) = vbNullString Or i = UBound(arrSource) Then Exit For 'will end the loop once the next row is empty
On Error GoTo 0
'If the next row substracted the first is greater than 1/24.05 both will be copied to the final array
If Not arrSource(i + 1, 2) - arrSource(i, 2) < target Then
For j = 1 To UBound(arrSource, 2)
finalArr(x, j) = arrSource(i, j)
finalArr(x + 1, j) = arrSource(i + 1, j)
Next j
x = x + 2 'increment 2 on x because you wrote 2 lines
End If
Next i
'paste the resulting array back to the sheet
.Range("A1", .Cells(UBound(finalArr), UBound(finalArr, 2))).Value = finalArr
'eliminate the extra unused rows
i = .Cells(.Rows.Count, 1).End(xlUp).row + 1
.Rows(i & ":" & .Rows.Count).Delete
End With
End Sub
Sub Change_dt()
单色暗靶
目标=板材(“控制器”)。单元格(16,9)。值
作为变体的源
使用此工作簿.Sheets(“原始数据”)
arrSource=.UsedRange.Value'这将在数组中输入整个使用过的工作表
模糊终结者作为变体
重拨最终用户(1至UBound(arrSource),1至UBound(arrSource,2))
.Cells.Delete'将清理工作表
尺寸i等于长,x等于长,j等于长
x=1
对于i=5至UBound(arrSource)
出错时继续下一步
如果arrSource(i+1,2)=vbNullString或i=UBound(arrSource),则“退出”将在下一行为空时结束循环
错误转到0
'如果下一行减去第一行大于1/24.05,则两行都将复制到最终数组
如果不是arrSource(i+1,2)-arrSource(i,2)<目标,则
对于j=1至UBound(arrSource,2)
最终(x,j)=arrSource(i,j)
最终(x+1,j)=arrSource(i+1,j)
下一个j
x=x+2'在x上增加2,因为您写了2行
如果结束
接下来我
'将生成的数组粘贴回工作表
.范围(“A1”,.单元格(UBound(finalArr),UBound(finalArr,2))。值=finalArr
'删除多余的未使用行
i=.Cells(.Rows.Count,1).End(xlUp).row+1
.Rows(i&“:”&.Rows.Count)。删除
以
端接头
如果您对行执行.clearcontents操作,然后进行排序,而不是删除行,则速度会更快。@Warcupine我不删除任何行,请删除整个工作表.Cells
并粘贴一个数组。谢谢您的回复。运行代码会在这一部分给我一个编译错误(“无效的下一个控制变量引用”):`如果i=1,那么对于j=1到UBound(arr,2)finalArr(x,j)=arrSource(i,j)Next i x=x+1`sorry@BartPostma一个小错误,在j的之后的第一个next i
是一个next j
谢谢你的快速回复@Damian,现在我在最后的“next i”上得到一个编译错误,说“next without for”。不知道为什么,因为我可以很清楚地看到“for”。我为自己是个大傻瓜而道歉。
Sub Change_dt()
Dim target As Single
target = Sheets("Controller").Cells(16, 9).Value
Dim arrSource As Variant
With ThisWorkbook.Sheets("Raw data")
arrSource = .UsedRange.Value 'this will input the whole used sheet inside the array
Dim finalArr As Variant
ReDim finalArr(1 To UBound(arrSource), 1 To UBound(arrSource, 2))
.Cells.Delete 'will clean the worksheet
Dim i As Long, x As Long, j As Long
x = 1
For i = 5 To UBound(arrSource)
On Error Resume Next
If arrSource(i + 1, 2) = vbNullString Or i = UBound(arrSource) Then Exit For 'will end the loop once the next row is empty
On Error GoTo 0
'If the next row substracted the first is greater than 1/24.05 both will be copied to the final array
If Not arrSource(i + 1, 2) - arrSource(i, 2) < target Then
For j = 1 To UBound(arrSource, 2)
finalArr(x, j) = arrSource(i, j)
finalArr(x + 1, j) = arrSource(i + 1, j)
Next j
x = x + 2 'increment 2 on x because you wrote 2 lines
End If
Next i
'paste the resulting array back to the sheet
.Range("A1", .Cells(UBound(finalArr), UBound(finalArr, 2))).Value = finalArr
'eliminate the extra unused rows
i = .Cells(.Rows.Count, 1).End(xlUp).row + 1
.Rows(i & ":" & .Rows.Count).Delete
End With
End Sub