有网友问到如何用VBA统计一行有多少非空的单元格。
在Excel中,用COUNTA函数就可以获取非空单元格。那么怎么在VBA中去统计非空的单元格呢?
这里分享自定义的函数,遍历选定的单元格,筛选空的单元格,再统计个数
Sub Count_Selection() Dim cell As Object Dim count As Long count = 0 For Each cell In Selection If IsEmpty(cell) = True Then count = count + 1 End If Next cell MsgBox "非空的单元格个数为:" & count ' MsgBox Application.CountA(Rows("1:2")) End Sub
效果图: