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

Excel:显示空白单元格地址

  •  0
  • Sevpoint  · 技术社区  · 7 年前

    我正在尝试在两列(e和f)中自动搜索空白单元格。我的目标是在一个消息框中显示这些空白单元格的地址。如果没有空白单元格,则会出现一个消息框,提示在两列中找不到空白单元格。到目前为止,我已经尝试了代码,但这只适用于第5(e)列,并且没有验证如果没有空白单元格,它将提示一条消息。我不知道如何才能继续实现我的目标。

    下面是我目前尝试的代码:

    Sub test()
    
    Dim i As Long, lastrow As Long
    Dim rng As Range
    Dim MsgStr As String
    Dim c As Range
    
    
    lastrow = Cells(Rows.Count, "E").End(xlUp).Row
    
    
    For i = 2 To lastrow
    
    If Cells(i, 5) = "" Then
    
            If MsgStr = "" Then
    
                MsgStr = Cells(i, 5).Address(False, False)
    
            Else
    
                MsgStr = MsgStr & "," & Cells(i, 5).Address(False, False)
    
            End If
    End If
    
    Next i
    
    MsgBox MsgStr & " cells are empty"
    
    End Sub
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   user4039065    7 年前

    尝试 SpecialCells(xlCellTypeBlanks) .

    Sub test()
        Dim lastrow As Long, rng As Range
        Dim MsgStr As String
    
        lastrow = Cells(Rows.Count, "E").End(xlUp).Row
        on error resume next
        set rng = range(cells(2, "E"), cells(lastrow, "F")).specialcells(xlcelltypeblanks)
        on error goto 0
    
        if not rng is nothing then
            msgbox rng.address(0,0) & " are blank"
        else
            msgbox "no blank cells"
        end if
    
    End Sub