object'_Global的方法'范围'在VBA中失败

我是新来的VBA,我有一个代码,我想要计算多less没有空行从A2到工作表的末尾,但我保持得到这个错误,代码如下:

我不能指出是什么问题

感谢帕吉

简单的修复:

 Sub TicketCopy() With Worksheets("Formulas") Dim K As Range Dim M As Long Set K = Range(.Range("A2"), .Range("A2").End(xlDown)) M = K.Rows.Count MsgBox "The Rows Count=" & M End With End Sub 

在这里输入图像说明

注意这是比列的底部less一个。

不清楚OP实际需要什么(“不是空白”单元或由公式产生的“非空白”单元,或者其他)

所以我只举几个例子:

 Option Explicit Sub TicketCopy() With Worksheets("Formulas") With .Range("A2", .Cells(.Rows.Count, 1).End(xlUp)) '<--| reference column "A" cells from row 2 to last not empty one ' all cells in range with formulas only With .SpecialCells(xlCellTypeFormulas) MsgBox "The Rows Count= " & .Count ' cells number MsgBox "The Rows Count= " & WorksheetFunction.Count(.Cells) ' cells resulting "not blank" (a 'zero' is a "not blank") MsgBox "The Rows Count= " & WorksheetFunction.CountIf(.Cells, "") ' cells resulting "blank" (a 'zero' is a "not blank") MsgBox "The Rows Count= " & WorksheetFunction.CountBlank(.Cells) ' cells resulting "blank" (a 'zero' is a "not blank") End With 'all cells in range MsgBox "The Rows Count= " & .Count ' cells number MsgBox "The Rows Count= " & WorksheetFunction.Count(.Cells) 'cells resulting "not blank" (a 'zero' is a "not blank") MsgBox "The Rows Count= " & WorksheetFunction.CountIf(.Cells, "") 'cells resulting "blank" (a 'zero' is a "not blank") MsgBox "The Rows Count= " & WorksheetFunction.CountBlank(.Cells) ' cells resulting "blank" (a 'zero' is a "not blank") End With End With End Sub