我正在构建一个宏,其中包括用于计算搜索结果的中间步骤。
如果搜索未得出结果,则该列保持空白,这会导致计数给出 1004 错误。
我的代码如下所示:
Sub Test()
Dim z As Integer
z=Worksheets("Pumps").Range("N:N").Cells.SpecialCells(xlCellTypeConstants).Count
Range("O1").Value = z
End Sub
有人知道如何控制它吗?
提前谢谢您。
最佳答案
使用 SpecialCells
进行错误处理。
另外,我删除了不必要的计数使用。
Dim rng1 As Range
On Error Resume Next
Set rng1 = Worksheets("Pumps").Range("N:N").Cells.SpecialCells(xlCellTypeConstants)
On Error GoTo 0
If Not rng1 Is Nothing Then Range("O1").Value = rng1.Cells.Count
关于excel - 计算空白列时避免 "Error 1004: no cells were found",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34505258/