簡體   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