excel - 使用隐藏单元格删除 VBA 中的值
问题描述
我正在尝试从 Excel 中的表中删除所有“0”值。我编写了以下代码,但它返回方法 '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
推荐阅读
- python - 在 Python 中找到任意函数的下确界/上确界以计算和绘制 darboux 和
- c# - 找不到任何可用的配置文件
- sql-server - 将 Outlook 电子邮件导出到 SQL(Vai ACCESS?)
- webapi - 在 Blazor 中,如何将图像加载到内存中以便 C# 可以访问它
- csv - 在 csv 中存储 Pyspark 数据创建问题
- swift - Firebase 正在接收请求,但视图不会显示数据
- c# - 工作单元更新问题:实体已经在跟踪另一个实例
- sql - DB中的向上和向下舍入问题
- python - 修复 sikuli 脚本中查找失败的问题
- sql - 未执行的捕获块