[英]Deleting Rows Based on Text Values in Specific Column
我編寫了一個簡短的宏來刪除第一列中所有值為“不適用”的行,用於我的工作簿的“預算”選項卡。
當我通過測試運行宏時,它似乎沒有做任何事情:
Sub Remove_NA_Macro_Round_2()
With Sheets("Budget") 'Applying this macro to the "Budget" sheet/tab.
'Establishing our macro range parameters
Dim LastRow As Long
Dim i As Long
'Setting the last row as the ending range for this macro
LastRow = .Range("I50").End(xlUp).Row
'Looping throughout all rows until the "LastRow" ending range set above
For i = LastRow To 1 Step -1
If .Range("I" & i).Value = "Not Applicable" Then
.Range("I" & i).EntireRow.Delete
End If
Next
End With
End Sub
我感謝任何幫助!
您實際上並未引用With Sheets("Budget")
。 添加句點.
在Range
每個實例之前,否則有一個隱式ActiveSheet
,它不一定是預算選項卡。
With Sheets("Budget")
...
LastRow = .Range("I50").End(xlUp).Row
...
If .Range("I" & i).Value = "Not Applicable" Then
.Range("I" & i).EntireRow.Delete
End If
...
End With
編輯:
根據評論和您提供的屏幕截圖,更改LastRow
的確定方式(去掉硬編碼的I50
):
LastRow = .Cells(.Rows.Count, "I").End(xlUp).Row
或者,根據條件刪除行時,使用過濾器比循環更快。
Dim rng As Range
Set rng = ThisWorkbook.Sheets("Sheet1").Range("A1:I" & Cells(Rows.Count, "I").End(xlUp).Row)
Application.DisplayAlerts = False
With rng
.AutoFilter
.AutoFilter field:=9, Criteria1:="Not Applicable"
rng.Resize(rng.Rows.Count - 1).Offset(1).SpecialCells(xlCellTypeVisible).Delete 'deletes the visible rows below the first row
.AutoFilter
End With
Application.DisplayAlerts = True
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.