当单元格中的值超过另一个值时,如何在excel vba中显示消息框?

当单元格中的值超过另一个值时,如何在excel vba中显示消息框?,excel,vba,Excel,Vba,当单元格G27中的值超过单元格K13中的值时,我需要显示一个消息框。我要求在G27单元格填满后立即显示。我尝试了以下宏,但它不起作用 Private Sub Worksheet_Change(ByVal Target As Range) If Range("G27") > Range("K13") Then MsgBox "error" End If End Sub 非常感谢您的帮助 这就是你正在尝试的吗?(未测试)也可以理解,您在工作表代码区域中有此代码

当单元格
G27
中的值超过单元格
K13
中的值时,我需要显示一个消息框。我要求在G27单元格填满后立即显示。我尝试了以下宏,但它不起作用

Private Sub Worksheet_Change(ByVal Target As Range)
    If Range("G27") > Range("K13") Then
        MsgBox "error"
    End If
End Sub

非常感谢您的帮助

这就是你正在尝试的吗?(未测试)也可以理解,您在工作表代码区域中有此代码,而不是模块:)

关于
工作表\u更改的更多信息

您也可以尝试此方法(数据验证):

与工作表(“工作表名称”)一起使用。
范围(“G27”)之前。如果在
专用子工作簿_Open()
下或在任何标准模块下使用,则验证。如果在
专用子工作表\u Activate()

供参考: 数据验证可以在没有vba的情况下使用,即直接从excel进行。您可以选择AlertStyle(信息、警告、停止)和输入标题。看

Private Sub Worksheet_Change(ByVal Target As Range)
    On Error GoTo Whoa

    Application.EnableEvents = False

    If Not Intersect(Target, Range("G27")) Is Nothing Then _
    If Range("G27").Value > Range("K13").Value Then MsgBox "error"

Letscontinue:
    Application.EnableEvents = True
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume Letscontinue
End Sub
With Range("G27").Validation
    .Delete
    .Add Type:=xlValidateWholeNumber, AlertStyle:=xlValidAlertStop, _
    Operator:=xlLessEqual, Formula1:="=K13"
    .IgnoreBlank = True
    .InCellDropdown = True
    .InputTitle = "Input Title"
    .ErrorTitle = "Error Title"
    .InputMessage = "Input Msg"
    .ErrorMessage = "Error Msg"
    .ShowInput = True
    .ShowError = True
End With