![](/img/trans.png)
[英]VBA code to show Message Box popup if the value of a cell exceeds the value of another
[英]How to Display a message box in excel vba when value in a cell exceeds another?
當單元格G27
的值超過單元格K13
的值時,我需要顯示一個消息框。 我要求在單元格G27填滿后立即顯示。 我已經嘗試了以下宏,但是它不起作用。
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("G27") > Range("K13") Then
MsgBox "error"
End If
End Sub
任何幫助深表感謝!
這是您要嘗試的嗎? ( 未經測試 ),也可以理解,你必須在表代碼區域的代碼,而不是一個模塊:)
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
更多關於Worksheet_Change
HERE
您也可以嘗試這樣做(數據驗證):
With Sheets("WorksheetName").
在Range("G27").Validation
如果在Private Sub Workbook_Open()
或任何標准模塊下使用,則進行Range("G27").Validation
。 如果在Private Sub Worksheet_Activate()
下使用,請按以下方式使用
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
僅供參考:可以在不使用vba的情況下使用數據驗證,即直接從excel中使用。 您可以選擇AlertStyle(信息,警告,停止)和Input Title。 請參閱http://office.microsoft.com/en-001/excel-help/apply-data-validation-to-cells-HP010342173.aspx
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.