Sub CountNonEmptyCells()
Dim lastRow As Long
Dim rangeToCount As Range
Dim nonEmptyCount As Long
' 定义你要计算的列范围
Set rangeToCount = Range("A1:A100")
' 获取最后一行
lastRow = rangeToCount.Cells(rangeToCount.Rows.Count).Row
' 遍历列中的每个单元格,计算非空单元格的数量
For Each cell In rangeToCount
If Not IsEmpty(cell) Then
nonEmptyCount = nonEmptyCount + 1
End If
Next cell
' 显示结果
MsgBox "非空单元格的数量为: " & nonEmptyCount
' 清除对象引用
Set rangeToCount = Nothing
End Sub
在代码中,你需要根据实际需求修改 rangeToCount
的范围。将其设置为你想要计算非空单元格的列范围。然后运行 CountNonEmptyCells
子过程,它将弹出一个消息框,显示非空单元格的数量。