我的代码一直有效,直到尝试过滤后删除所有可见的单元格(标题除外)为止。有人可以告诉我为什么我的offset命令给我一个应用程序定义的错误或对象定义的错误吗?
我尝试仅使用行,删除可见单元格,设置范围。
Async/Await
我要在过滤后删除所有可见的单元格(标题除外)。
答案 0 :(得分:0)
不是答案,但将其分解一下,看看是否可以调试:
Dim rng As range
With targetSheet.Range(targetRangeName)
.AutoFilter Field:=17, Criteria1:="<>" & sourceCell.Value, _
Operator:=xlFilterValues
Set rng = .Offset(1, 0)
Debug.Print rng.Address
Set rng = rng.SpecialCells(xlCellTypeVisible)
Debug.Print rng.Rows.Count
rng.EntireRow.Delete
End With
编辑:如果范围已到达工作表的最后一行,则不能将其偏移一行。如果您只想排除标题行,则可以执行以下操作:
Dim rng
Set rng = ActiveSheet.Columns(1)
Debug.Print rng.Address '>> $A:$A
'resize and offset to exclude the first row
Set rng = rng.Resize(rng.Rows.Count - 1, rng.Columns.Count).Offset(1, 0)
Debug.Print rng.Address '>> $A$2:$A$1048576