2013-04-08 312 views
2

嗨,我试图计算包含空白单元格的行数。 (我知道有963个空白单元格,我只是不知道它们分布在多少行)计算空白单元格的行数(Excel/VBA)

我对VBA的知识非常有限,而且发现很难实现。

我在想的方式...

两个for循环。

外环将循环向下行

每个单元

内循环将循环排

当在一排中遇到一个空白单元格计数器将递增一,我们将移动到下一行。

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

非常感谢

回答

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 
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

+ 1你打我:) – 2013-04-08 18:46:54

+0

@Siddharth溃败** **这并不经常发生。 – tbur 2013-04-08 18:54:08

+1

:)我不得不删除我的帖子,因为这个;) – 2013-04-08 18:54:50

4

这里有一个很简单的方法来做到这一点不VBA:

Example1

+0

+1对于非VBA解决方案:)您可能需要更改它,以便显示Col E而不是'1'的空白总数? – 2013-04-08 18:49:48

+0

您链接的图像已损坏。这就是为什么最好不要这样做。 – warsong 2017-08-15 08:16:59

相关问题