计算空白单元格的行数(Excel / VBA)
嗨,我试图计算包含空白单元格的行数。 (我知道有963个空白单元,我只是不知道它们分布在多less行)
我对VBA的知识非常有限,而且发现很难实现。
我在想…
两个for循环。
外循环将沿着行循环
内循环将循环在行中的每个单元格上
当一行中遇到空白单元格时,计数器将递增1,然后移动到下一行。
如果任何人可以帮助我实现这一点,我将不胜感激。
非常感谢
这里有一个相当简单的方法来做到这一点,没有VBA:
你实际上不需要任何循环来做到这一点。
此示例检查行A.将“Const column_to_test”编号更改为您希望检查空白单元格的列编号。
Sub countblank() 'This will count the number of rows that have a blank cell in column "A" Const column_to_test = 1 'first column (A) Dim r As Range Set r = Range(Cells(1, column_to_test), Cells(Rows.Count, column_to_test).End(xlUp)) MsgBox ("There are " & r.SpecialCells(xlCellTypeBlanks).Count & " Rows with blank cells") 'You may want to select those rows (for deletion?) r.SpecialCells(xlCellTypeBlanks).EntireRow.Select 'change .Select to .Delete End Sub
尝试下面的代码
Sub countBlankInRow() Dim counter As Long For i = 1 To 1000 ' specify your rows For j = 1 To 26 ' specify your columns If Cells(i, j) <> "" Then Exit For Else If j = 26 Then counter = counter + 1 ' Alter the col no accordingly End If Next Next MsgBox counter End Sub