使用隐藏的单元格删除VBA中的值

我正在尝试从excel表中删除所有“ 0”值。我编写了以下代码,但它返回方法“对象'_Worksheet'的范围”失败。我该怎么做才能解决此问题?

Sub Macro()

Dim ws As Worksheet          
''Set reference
Set ws = ThisWorkbook.Worksheets("Compressed Schedule results")

''Apply Filter
    ws.Range("A2:B2").AutoFilter Field:=1,Criteria1:="0"
 lrow = activeSheet.Cells(Rows.Count,"A").End(xlUp).SpecialCells(xlCellTypeVisible).Row
''Delete the Rows
Application.DisplayAlerts = False
ws.Range("A2:lrow").SpecialCells(xlCellTypeVisible).Delete
Application.DisplayAlerts = True


ws.ShowAllData



End Sub
guojinfei 回答:使用隐藏的单元格删除VBA中的值

正如@BigBen所指出的,您错误地引用了带有变量的范围。设置最后一行时,我还删除了SpecialCells

Sub Macro()
Dim ws As Worksheet
Dim lRow As Long
''Set reference
Set ws = ThisWorkbook.Worksheets("Compressed Schedule results")
lRow = ws.Cells(Rows.Count,"A").End(xlUp).Row  
''Apply Filter  
ws.Range("A2:B2").AutoFilter Field:=1,Criteria1:="0"
''Delete the Rows
Application.DisplayAlerts = False
ws.Range("A2:A" & lRow).SpecialCells(xlCellTypeVisible).EntireRow.Delete
Application.DisplayAlerts = True
ws.ShowAllData
End Sub
本文链接:https://www.f2er.com/2775510.html

大家都在问