代码之家  ›  专栏  ›  技术社区  ›  gizgok

如何在vba中找出整行是否为空

  •  13
  • gizgok  · 技术社区  · 14 年前

    我有一张表,里面有来自两个不同来源的数据。它们之间有一个空行。我想把这个空行作为我的分隔符。怎么做我能看看整行是不是空白吗。

    3 回复  |  直到 14 年前
        1
  •  25
  •   GotDibbs    14 年前

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

    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 )

        2
  •  14
  •   Jay    14 年前

    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