vba 如果满足某些条件则弹出消息框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15092576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Popup message box if certain criteria are met
提问by Jamie Walker
I am working on building a cost sheet. Each of the cost line items I put in will have a validity date on which the cost needs to be updated. I am trying to get a message box to popup to notify me when a cost is past it's validity date. In my spreadsheet the validity date is on "Quote Sheet" in column D and I have the actual date on the sheet "Data Entry" in cell B2. I also have check boxes to select which cost items you want included which when selected populate column Q with a True or False. What I am looking to do is do a macro that will run when an item is selected. So the wording would go something like, "If column Q = True and if column D is less than cell B2 on "Data Entry" then MsgBox "Price is out of date!"" I also have subtotals which will have blanks for the date so I would need something saying if column D is blank then disregard. Thanks for any help! I found the below online and tried it but it didn't work.
我正在制作成本表。我输入的每个成本行项目都有一个需要更新成本的有效日期。我试图让一个消息框弹出,以在费用超过其有效日期时通知我。在我的电子表格中,有效日期在 D 列的“报价单”上,我在单元格 B2 的“数据输入”表上有实际日期。I also have check boxes to select which cost items you want included which when selected populate column Q with a True or False. 我想要做的是做一个宏,该宏将在选择项目时运行。因此,措辞将类似于,“如果列 Q = True 并且如果列 D 小于“数据条目”中的单元格 B2,则 MsgBox“价格已过时!” 我也有小计,日期会有空白,所以我需要一些说明,如果 D 列是空白,则忽略。谢谢你的帮助!我在网上找到了以下内容并尝试了它,但没有用。
Private Sub Worksheet_Change(ByVal Target As Range)
'Only run if change made to D3, and D2 and D3 are not = ""
If Intersect(Target, Range("D:D")) Is Nothing Then Exit Sub
If Range("D:D").Value < Sheets("Data Entry").Range("B2").Value Then
MsgBox "Cost is out of date!"
End If
End Sub
回答by Excel Developers
You need to add a condition to your first IF construct to check for blank cells, and Range("D:D") in your second IF construct to Target:
您需要在第一个 IF 构造中添加条件以检查空白单元格,并将第二个 IF 构造中的 Range("D:D") 添加到 Target:
Private Sub Worksheet_Change(ByVal Target As Range)
'Only run if change made to D3, and D2 and D3 are not = ""
If Intersect(Target, Range("D:D")) Is Nothing or IsEmpty(Target) Then Exit Sub
If Target.Value < Sheets("Data Entry").Range("B2").Value Then
MsgBox "Cost is out of date!"
End If