所以我正在编写一个VBA来尝试自动化一些数据分析,这将循环遍历数据,并且只要行中的时间差超过一秒延迟(数据分辨率更高),它就会添加一个空白行表示数据的新“测试运行”。然后我想删除空行之间的任何行(调用RangeA),如果RangeA说2秒(即一个没有意义的短测试运行)。
我设法创建了一些添加空行的气质代码,但在我的if语句中返回“type mismatch”。
我之后需要根据这些数据创建一个图表,所以我不确定添加空行是否是最好的方法,否则以后会引起问题。
编辑 - 发现有些单元格中有一些字符串,因为我之前已经弄乱了一些宏。所以它现在用空行分隔数据,现在尝试消除每个块中少于2秒的任何内容。
Sub dataSeperator()
Dim rowStart As Long
Dim rowEnd As Long
Dim rowLoop As Long
Dim FindColumn As Range
rowStart = 3
rowEnd = Sheets("Data").UsedRange.Rows(Sheets("Data").UsedRange.Rows.Count).row
With Sheets("Data")
Set FindColumn = Cells.Find(What:="Time", After:=.Cells(1, 1), LookIn:=xlValues, LookAt:= _
xlWhole, MatchCase:=False)
End With
For rowLoop = rowEnd To rowStart Step -1
With Sheets("Data").Cells(rowLoop, FindColumn.Column)
If Cells(rowLoop - 1, FindColumn.Column) - Cells(rowLoop, FindColumn.Column) < -1 Then
.EntireRow.Insert
End If
End With
Next rowLoop
End Sub
答案 0 :(得分:1)
我希望这会有所帮助。
Seperator()
Dim rowStart As Long
Dim rowEnd As Long
Dim rowLoop As Long
Dim FindColumn As Range
rowStart = 3
With Sheets("Data")
rowEnd = .UsedRange.Rows(.UsedRange.Rows.Count).row
' replaced "Cells" with ".cells"
Set FindColumn = .Cells.Find(What:="Time", After:=.Cells(1, 1), LookIn:=xlValues, LookAt:= xlWhole, MatchCase:=False)
End With
For rowLoop = rowEnd To rowStart Step -1
With Sheets("Data").Cells(rowLoop, FindColumn.Column)
' Used .Value instead
' "Cells" refers to the active sheet!
' use Sheets("Data").Cells instead
If Sheets("Data").Cells(rowLoop - 1, FindColumn.Column) - .value < -1 Then
' If Cells(rowLoop - 1, FindColumn.Column) - Cells(rowLoop, FindColumn.Column) < -1 Then
.EntireRow.Insert
End If
End With
Next rowLoop
End Sub
答案 1 :(得分:1)
我的第一个答案仍然存在,但在我看来,如果你按照以下方式工作,你可以提高代码的可读性和简单性。你怎么看?:
Sub Seperator2()
Const TableHeaderRowNumber As Long = 1
Dim cellTableHeaderWithTime As Range
Dim rngMyTable As Range
Dim rngMyColumnOfTimes As Range
Dim rowStart As Long
Dim rowEnd As Long
Dim lngCounter As Long
With Sheets("Data")
Set cellTableHeaderWithTime = .Cells.Find(What:="Time", After:=.Cells(TableHeaderRowNumber, 1) _
, LookIn:=xlValues _
, LookAt:=xlWhole _
, MatchCase:=False)
rowStart = TableHeaderRowNumber + 2
rowEnd = .UsedRange.Rows(.UsedRange.Rows.Count).Row
Set rngMyTable = .Range(.Cells(rowStart, cellTableHeaderWithTime.Column), .Cells(rowEnd, cellTableHeaderWithTime.Column))
' Just get the column of cells you need to compare
Set rngMyColumnOfTimes = Intersect(rngMyTable, cellTableHeaderWithTime.EntireColumn)
For lngCounter = rngMyColumnOfTimes.Cells.Count To rowStart Step -1
'rngMyTable(lngCounter) is shorthand for rngMyTable.item(lngCounter)
With rngMyTable(lngCounter)
Debug.Print .Address
If .Offset(-1, 0) - .Value < -1 Then
.EntireRow.Insert
End If
End With
Next lngCounter
End With
End Sub