Microsoft Excel 2010: 从月到月,数据行数可以变化。当我将新数据粘贴到ILS_IMPORT选项卡时,可能有3,500条记录,下个月可能是2,500条。当我将数据导入Access时,除非我从第2,501行删除所有记录,否则将出现额外的1,000行。我想让Excel VBA做到这一点并尝试过,但到目前为止还没有任何工作。我知道列O总是会有数据到底,因为它是四分之一指标(例如Q2)。
但是,此代码会一直删除最后一行,而且我不知道它是否真的一直删除到最后一行。有人能指出我正确的方向吗?
Sub test()
Dim rng As Range
Dim lastRow As Long
With ThisWorkbook.Sheets("ILS_IMPORT")
'Find anything in the cells
Set rng = .Cells.Find(What:="*", _
After:=.Range("O1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False)
'if data is NOT found - exit from sub
If rng Is Nothing Then Exit Sub
'find last row
If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
lastRow = .Cells.Find(What:="*", _
After:=.Range("O1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
Else
lastRow = 1
End If
'I use lastRow + 1 to prevent deletion data above when it is on lastrow
.Range(rng.Row + 2 & ":" & lastRow + 2).Delete Shift:=xlUp
End With
End Sub
答案 0 :(得分:0)
您可以在将数据粘贴到?
之前清除/删除空白范围range(cells(2,1),cells(2,1).end(xldown)).EntireRow.Clear