【问题标题】:VBA: Message Box Display, Loop on ConditionVBA:消息框显示,条件循环
【发布时间】:2018-08-22 05:23:38
【问题描述】:

我创建了在满足特定条件时显示消息框的 VBA 代码。此 if 语句在确定的范围内循环。但是,每个满足我的条件的单元格都会显示消息框。有没有办法确保消息框只显示一次?

这是我的代码:

Private Sub Worksheet_Change(ByVal Target As Range)
For Each cell In Range("S194:BZ194")
    If cell.Value < 0 Then
MsgBox "Unrestricted cash cannot be less than zero (Row 194). Please lower the loan growth rate."
End If
Next
End Sub

【问题讨论】:

  • 如果您只想通过工作表更改事件 Exit For 显示一次 for 循环,请退出您的 for 循环。如果您只想在每个实例中显示一次,则创建一个全局变量来保存布尔值。
  • 看起来您正在尝试实现穷人的数据验证。您正在重新发明轮子,请查看 [数据] 功能区选项卡。

标签: vba loops conditional messagebox


【解决方案1】:
Private Sub Worksheet_Change(ByVal Target As Range)
    For Each cell In Range("S194:BZ194")
        If cell.Value < 0 Then
            MsgBox "Unrestricted cash cannot be less than zero (Row 194). Please lower the loan growth rate."
            Exit For
        End If
    Next
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    • 2020-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多