excel - 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
解决方案
试试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
推荐阅读
- c++ - 在 pybind11 中包装可变参数模板
- python - 如何将日期时间信息写入 netcdf4?
- angular - 如何测试角度服务承诺调用?
- asp.net - 从 Visual Studio 运行 ASP.NET Core 项目
- uml - 用例图 - 多次显示参与者
- javascript - 无论如何在VSC中连接和编辑MySql?
- python - pytorch 在 anaconda 提示符下运行,但不在 python idle 中运行
- docker - Kubernetes OOM pod 被杀死,因为内核内存增长太多
- php - 我应该怎么做才能符合 MVC 范式?
- sql - SQL - 为不同的列 1 选择限制 column2 值