繁体   English   中英

如果目标列中的公式超过某个值,则显示消息框弹出窗口的VBA代码

[英]VBA code to show message box popup if the formula in the target column exceeds a certain value

我在此网站上找到了特定单元格的代码

Private Sub Worksheet_Change(ByVal Target As Range)
If Range("A1") > 0.5 Then
    MsgBox "Discount too high"
End If
End Sub

但是我想知道是否有可能使整个列而不是一个特定的单元格都可以进行这项工作?

尝试使用此事件代码:

Private Sub Worksheet_Calculate()
    Dim rr As Range, r As Range
    Set rr = Range("A:A").Cells.SpecialCells(xlCellTypeFormulas)
    For Each r In rr
        If r.Value > 0.5 Then
            MsgBox "Discount too high"
        End If
    Next r
End Sub

编辑#1:

如果要将消息限制为一行,请删除第一个子并将其替换为:

Private Sub Worksheet_Change(ByVal Target As Range)
    rt = Target.Row
    If Range("A" & rt) > 0.5 Then
        MsgBox "Discount too high"
    End If
End Sub

我会说这是在Excel中做事的错误方式:)。

我可以提出以下建议:1.根据值,例如> 0.5,将条件格式添加到单元格中。2.在该单元格旁边的单元格中添加一个简单的IF公式,该公式表示错误。

使用事件将抑制撤消操作

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM