我使用下面的代码来计算单元格的数量。
Range(Range("K2"), Range("K2").End(xlDown)).Count
然而,单元格中原本包含"=IF "公式,所以代码返回的单元格数不正确(它返回的是4xxx,而不是我想要的 "60")。
另外,我还尝试使用
Cells(Rows.Count, "K").End(xlUp).Row
虽然它返回的数字要小得多(即75),但它也包括了行中的一些空单元格(61-65)。 我怎样才能得到我想要的数字(即那些空单元格前的60),谢谢。
一句话(不建议)
NumberOfRows = Range(Cells(2, "K"), Columns("K").Find(What:="", _
After:=Cells(1, "K"), LookIn:=xlValues).Offset(-1)).Rows.Count
或参考以下2个子的代码。
NumberOfRows = Range(Cells(HeaderRow + 1, Col), Columns(Col).Find(What:="", _
After:=Cells(HeaderRow, Col), LookIn:=xlValues).Offset(-1)).Rows.Count
更详细的解释
Sub CountRows()
Const Col As Variant = "K" ' Source Column Number/Letter
Const HeaderRow As Long = 1 ' Header Row
Dim rng As Range ' Last Cell, Target Range
Dim NumberOfRows As Long ' Number of Rows
' Define Last Cell.
Set rng = Columns(Col).Find(What:="", After:=Cells(HeaderRow, Col), _
LookIn:=xlValues).Offset(-1)
' Define Target Range and write its number of rows to Number of Rows.
' Display the result in the Immediate window.
If Not rng Is Nothing Then
Set rng = Range(Cells(HeaderRow + 1, Col), rng)
NumberOfRows = rng.Rows.Count
Debug.Print "Number of Rows = " & NumberOfRows
Else
MsgBox "No empty cells" ' Highly unlikely.
End If
End Sub
更详细的解释
Sub CountRowsStudy()
Const Col As Variant = "K" ' Source Column Number/Letter
Const HeaderRow As Long = 1 ' Header Row
Dim rng As Range ' First Blank Cell Range, Target Range
Dim NumberOfRows As Long ' Number of Rows
Debug.Print Columns(Col).Address
Debug.Print Cells(HeaderRow, Col).Address
' 'Columns(Col)' means the whole column whatever Col is. In this case
' Col is declared as Variant so either "K" or 11 can be used.
' 'Cells(HeaderRow, Col)' means at the intersection of Header Row
' and Source Column, i.e. range "K1". The 'After' argument when used
' with the omitted default parameter ('xlNext') of the 'SearchDirection'
' argument, starts the search one cell after the cell used in the
' 'After' argument, i.e. after "K1" which will then first 'look' into "K2"
' which also might be empty. The xlValues parameter of the LookIn argument
' will ensure to stop at the first empty cell whether it is blank
' or has a formula evaluating to "" in it.
' Define Last Cell i.e. Last Non-Empty Cell Range before
' the First Empty Cell Range i.e. find the first blank cell or
' the cell containing a formula evaluating to "" and use offset to go
' up one cell because you don't want the found empty cell to be counted.
Set rng = Columns(Col).Find(What:="", After:=Cells(HeaderRow, Col), _
LookIn:=xlValues).Offset(-1)
Debug.Print rng.Address
' Check if the column is not empty (full).
If Not rng Is Nothing Then
' Define Target Range. It's starting from the same cell as the "After"
' argument, only one row down (we don't want the headers included),
' and it ends with the previously defined Last Non-Empty Cell Range.
Set rng = Range(Cells(HeaderRow + 1, Col), rng)
Debug.Print rng.Address
' Count the Number of Rows in Target Range.
NumberOfRows = rng.Rows.Count
Debug.Print "Number of Rows = " & NumberOfRows
Else
' Inform user.
MsgBox "No empty cells" ' Highly unlikely.
End If
End Sub