简体   繁体   English

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

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

I found this code on this site for a particular cell 我在此网站上找到了特定单元格的代码

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

But I was wondering if it is possible to make this work for an entire column rather than one particular cell? 但是我想知道是否有可能使整个列而不是一个特定的单元格都可以进行这项工作?

Try this for your event code: 尝试使用此事件代码:

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

EDIT#1: 编辑#1:

if you want to restrict the message to a single row, then remove the first sub and replace it with: 如果要将消息限制为一行,请删除第一个子并将其替换为:

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

I would say this is the wrong way of doing thing in Excel :). 我会说这是在Excel中做事的错误方式:)。

Can I suggest the following: 1. Add Conditional Formatting to cells depending on the values eg > 0.5 2. Add in the cell next to this cell a simple IF formula that signal the error. 我可以提出以下建议:1.根据值,例如> 0.5,将条件格式添加到单元格中。2.在该单元格旁边的单元格中添加一个简单的IF公式,该公式表示错误。

Using events will suppress undo operations 使用事件将抑制撤消操作

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

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