如何计算已满的单元格数并获得返回编号
例如:范围(“A4”),然后计算其下具有文本并返回计数的单元格数。点击没有文字的单元格时必须停止。
L = CountA("A4").End(xlDown)
答案 0 :(得分:0)
编辑:根据评论,这是正确的代码:
简单End(xlDown)
方法
Sub GetCellCount()
With ThisWorkbook.Sheets("Sheet1") 'Modify as necessary.
Set HeadC = .Range("A4")
CellCt = HeadC.End(xlDown).Row - HeadC.Row
MsgBox CellCt
End With
End Sub
结果:
如果有帮助,请告诉我们。
EDIT2:对上述内容不满意,因为简单的End(xlDown)
方法可能会在将来造成潜在问题。其他方法如下。
检查空单元格
Sub GetCellCountv2()
With ThisWorkbook.Sheets("Sheet1") 'Modify as necessary.
CellCt = 0
Set HeadC = .Range("A4") 'Modify as necessary.
For Iter = HeadC.Row + 1 To .Rows.Count
If Not IsEmpty(.Cells(Iter, HeadC.Column)) Then
CellCt = CellCt + 1
Else
Exit For
End If
Next Iter
MsgBox CellCt
End With
End Sub
检查字符串方法的长度
Sub GetCellCountv3()
With ThisWorkbook.Sheets("Sheet1") 'Modify as necessary.
CellCt = 0
Set HeadC = .Range("A4") 'Modify as necessary.
For Iter = HeadC.Row + 1 To .Rows.Count
If Len(.Cells(Iter, HeadC.Column).Value) > 0 Then
CellCt = CellCt + 1
Else
Exit For
End If
Next Iter
MsgBox CellCt
End With
End Sub
希望这可以解决请求中的几个潜在问题。