简体   繁体   中英

Popup message box if certain criteria are met

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.

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

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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