问题
I am trying to make a simple countRows
function that will count the number of cells I have in a dynamic range. Basically if I have values in cells, say B2:B500, the count would return 499. However next time around values are in cell B2:B501, the count would return 500. But you wouldn't have to do anything to the cell in which you typed in the formula.
I thought if I reference the cell as a Variant
, then any value could be accepted. Then find the Address
of that cell and return the Count
of a Range
. But I get a #Value
error.
Public Function countRows(startRange As Variant)
Dim rng As Range
Set rng = startRange.Address
If IsEmpty(Range(rng, rng.End(xlDown))) = True Then
countRows = 1
Else
countRows = Range(rng, rng.End(xlDown)).Rows.Count
End If
End Function
回答1:
This is the code I have used for many years successfully under many different worksheets. It handles many cells, singular cells or empty cells.
Public Function CountRows(ByRef r As Range) As Long
If IsEmpty(r) Then
CountRows = 0
ElseIf IsEmpty(r.Offset(1, 0)) Then
CountRows = 1
Else
CountRows = r.Worksheet.Range(r, r.End(xlDown)).Rows.count
End If
End Function
Public Function CountCols(ByRef r As Range) As Long
If IsEmpty(r) Then
CountCols = 0
ElseIf IsEmpty(r.Offset(0, 1)) Then
CountCols = 1
Else
CountCols = r.Worksheet.Range(r, r.End(xlToRight)).Columns.count
End If
End Function
回答2:
It's not entirely clear what you are looking for, when you mentioned there are values in cells "B2:B500" and the count should return 499, as there could be a few possible scenarios:
Range("B2:B500").Rows.Count
WorksheetFunction.CountA(Range("B2:B500"))
rng.End(xlDown)
, you probably want to the count continuous non-blank cells starting with the range "B2" in the overall range "B2:B500". You may create a function like this:Public Function countRows(rng As Range) As Long
Dim rw As Range
For Each rw In rng
If IsEmpty(rw) Then Exit For
countRows = countRows + 1
Next
End Function
Clarification:
Based on subsequent comments, I thought it's worth explaining why the variable "countRows" wasn't initialized by adding a line countRows = 0
.
Certain programming languages like assembly language, C, C++ require explicit initialization. This was intentionally so designed due to the philosophy in which conflicts between performance and safety were generally resolved in favor of performance.
However, such is not the case with other programming languages like VBA or Java.
Speaking about VBA, during macro run, all the variables are initialized to a value. A numeric variable is initialized to zero, a variable length string is initialized to a zero-length string (""), and a fixed length string is filled with the ASCII code 0. Variant variables are initialized to Empty. An Empty variable is represented by a zero in a numeric context and a zero-length string ("") in a string context.
Therefore a separate line of code countRows = 0
wasn't added in the above code block.
While coding, one need to keep this in perspective as the same might not be true for other languages.
来源:https://stackoverflow.com/questions/45391853/making-a-countrows-function-in-excel