You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在VBA的For循环中将当前遍历的单元格显示在MsgBox中

VBA中在MsgBox显示当前遍历的单元格信息

你的问题出在直接将Cell对象和字符串拼接,VBA会默认返回单元格的Value属性(也就是0),而不是单元格的地址/引用信息。要显示单元格位置,需要调用单元格对象的Address属性。

修改方案

方案1:显示绝对引用(如$C$5)

把MsgBox行改成:

MsgBox "Please input a number for cell " & Cell.Address & " that isn't 0."

方案2:显示相对引用(如C5,更直观)

如果想要不带美元符号的相对地址,给Address方法传两个False参数:

MsgBox "Please input a number for cell " & Cell.Address(False, False) & " that isn't 0."

完整修改后的代码

For Each Cell In Range("C5:C11")
    If Cell.Value = 0 Then
         MsgBox "Please input a number for cell " & Cell.Address(False, False) & " that isn't 0."
    End If
Next Cell

注意:原代码遗漏了循环结束语句Next Cell,补上后才能正常编译运行。

内容的提问来源于stack exchange,提问作者Kevin Angel Fitzgerald Gomez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 06:14:51