我正在尝试自动搜索两列(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
答案 0 :(得分:0)
尝试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