从 Access 导入过程中删除最后一行数据下方的所有行
Delete all rows below the last row of data for import process from Access
微软 Excel 2010:
每个月,数据的行数都是可变的。当我将新数据粘贴到 ILS_IMPORT 选项卡时,可能有 3,500 条记录,下个月可能是 2,500 条。当我去将数据导入Access时,会出现额外的1,000行,除非我从第2,501行开始删除所有记录。我想让 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
你能clear/delete粘贴数据之前的空白区域吗?
range(cells(2,1),cells(2,1).end(xldown)).EntireRow.Clear
微软 Excel 2010: 每个月,数据的行数都是可变的。当我将新数据粘贴到 ILS_IMPORT 选项卡时,可能有 3,500 条记录,下个月可能是 2,500 条。当我去将数据导入Access时,会出现额外的1,000行,除非我从第2,501行开始删除所有记录。我想让 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
你能clear/delete粘贴数据之前的空白区域吗?
range(cells(2,1),cells(2,1).end(xldown)).EntireRow.Clear