首页 文章

计算空白单元格的行数(Excel / VBA)[关闭]

提问于
浏览
2

嗨,我正在尝试计算包含空白单元格的行数 . (我知道有963个空白单元格,我只是不知道它们分散了多少行)

我对VBA的了解非常有限,并且发现很难实现 .

我在想的方式......

两个for循环 .

外循环将循环向下行

内循环将循环遍历行中的每个单元格

当一行中遇到空白单元格时,计数器将递增一,我们将移动到下一行 .

如果任何人可以帮助我实现这一点,我将不胜感激 .

非常感谢

3 回答

  • 4

    在没有VBA的情况下,这是一个相当简单的方法:

    Example1

  • 3

    你实际上不需要任何循环来做到这一点 .

    此示例检查行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
    
  • 0

    试试下面的代码

    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
    

相关问题