首页 > 解决方案 > Excel:显示空白单元格地址

问题描述

我正在尝试自动搜索两列(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

标签: excelvba

解决方案


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

推荐阅读