[英]VBA - remove all rows until it finds requested value
你能告訴我,我在下面的代碼中我該怎么做?
我有可變數據量的excel文件。 數據表已拆分,我需要刪除其第二部分。 數據的第二部分以A列中的“不在Ico貸款工具中的一方或雙方”開頭。因此,基本上,我認為我會從后面開始查找並刪除每一行,直到找到包含搜索短語的單元格為止,然后它會停止。
該代碼刪除了直到A3的所有內容,包括數據的第一部分。 請問您有什么建議嗎?
Private Sub Remove_part2()
Dim i As Long
Dim LastRow As Long
Dim r As Range
With Worksheets("Hyperion Data (Original)")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
Set r = Range("A3", "A" & Rows.Count).Find(What:="One or both Parties Not in Ico Loan Facility", LookIn:=xlValues, LookAt:=xlPart, SearchDirection:=xlNext, MatchCase:=False)
For i = LastRow To 3 Step -1
If Not r Is Nothing Then
ActiveSheet.Rows(i).EntireRow.Delete
End If
Next i
End With
End Sub
怎么樣:
Private Sub Remove_part2()
Dim i As Long
Dim LastRow As Long
Dim r As Range
Dim ws As Worksheet: Set ws = Worksheets("Hyperion Data (Original)")
With ws
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
Set r = .Range("A3", "A" & .Rows.Count).Find(What:="One or both Parties Not in Ico Loan Facility", LookIn:=xlValues, LookAt:=xlPart, SearchDirection:=xlNext, MatchCase:=False)
If Not r Is Nothing Then
For i = LastRow To r.Row Step -1
ws.Rows(i).EntireRow.Delete
Next i
Else
MsgBox "Criteria Not Found!", vbInformation
End If
End With
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.