如何通过VBA在Excel中找出整行是否为空

22

我有一个表格,其中包含来自两个不同来源的数据。它们之间有一行空行,我希望将这个空行作为我的分隔符。如何判断整行是否为空?

2个回答

29
如果您要处理整个行的话,类似以下代码应该可以工作(前提是单元格中没有公式或空格):
If Application.CountA(ActiveCell.EntireRow)=0 Then
     MsgBox "Row Empty"
     Exit Sub
End If

否则,对于从一行开始的范围:
Dim neValues As Range, neFormulas As Range, MyRange As Range

Set MyRange = Columns("C:AA")

On Error Resume Next
Set neValues = Intersect(ActiveCell.EntireRow.SpecialCells(xlConstants), MyRange)
Set neFormulas = Intersect(ActiveCell.EntireRow.SpecialCells(xlFormulas), MyRange)
On Error GoTo 0

If neValues Is Nothing And neFormulas Is Nothing Then
    MsgBox "Nothing There"
Else
    MsgBox "Something's There"
End If

(来源:http://www.ozgrid.com/forum/showthread.php?t=26509&page=1

的意思是:


16

如下所示,WorksheetFunction.CountA()函数可以实现:

Dim row As Range
Dim sheet As Worksheet
Set sheet = ActiveSheet

For i = 1 To sheet.UsedRange.Rows.Count

    Set row = sheet.Rows(i)
    If WorksheetFunction.CountA(row) = 0 Then
        MsgBox "row " & i & " is empty"
    End If

Next i

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接