簡體   English   中英

VBA中對象“ _Global”的方法“范圍”失敗

[英]Method 'Range' of object'_Global' Failed in VBA

我是VBA的新手,我有一個代碼,我想計算從A2到工作表末尾沒有多少空行,但是我一直在收到此錯誤,並且代碼如下:

我無法指出問題所在

謝謝帕吉

簡單修復Set

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

在此處輸入圖片說明

注意,這比列的底部小一。

不清楚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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM