如何通过vba查看excel中整行是否为空

时间:2010-09-02 14:23:37

标签: excel vba

我有一张表,其中我有来自两个不同来源的数据。我们之间有一个空行。我想把这个空白行作为我的分隔符。如何找出整行是否为空

3 个答案:

答案 0 :(得分:21)

如果你正在说一个字面的整行,那么与此类似的代码应该可以工作(只要在任何单元格中都没有公式或空格):

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

答案 1 :(得分:11)

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

答案 2 :(得分:-1)

我试过这个并且它有效。

Option Explicit

Sub Dl_BlankRow()
Application.ScreenUpdating = False
Dim SelectRange As Range
Dim TempRange As Range
Dim i As long
Set SelectRange = Application.Selection
Set TempRange = Intersect(SelectRange, ActiveSheet.UsedRange)
For i = TempRange.Rows.Count + TempRange.Row - 1 To 1 Step -1
If Cells(i, 1).End(xlToRight).Column = 16384 And Cells(i, 1) = "" Then
Rows(i).Delete
End If
Next
Application.ScreenUpdating = True
End Sub