删除 VBA 中包含隐藏单元格的值

Delete values in VBA with hidden Cells

我正在尝试从 excel 中的 table 中删除所有“0”值。我编写了以下代码,但是 returns 方法 'Range of object'_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

正如@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