How can I show an error message through data validation when the cell is auto populated

kjohn25744

New Member
Joined
Oct 11, 2021
Messages
8
Office Version
  1. 365
Platform
  1. Windows
I am building an order form, and I am having problems displaying an error message when a value in a cell is auto populated. (See diagram below) I have created a data validation on the list price, but I cannot get it to trigger when the field is populated via a VLOOKUP formula. I need an error message to be displayed so the customer knows that item is not available and to choose another item. (Note some items are available in a different color.

1722003731656.png
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
If you don't mind using code, you should be able to use the worksheet calculate event. So user makes a selection and you check if the offset of 4 columns from the active cell contains an X and then open a message box? Or does the user do something in between the validation selection and the calculation that results in X?
 
Upvote 0
The customer will enter the cabinet name then formals calculate the item code and the list price. So yes, I think your solution could work, but I know just enough VBA to be dangerous. The Item Name will be in column C, the item code is in column F and the List Price is in column G.
 
Upvote 0
So if it works like this
- I use drop-down validation, formula recalculates immediately
then it should work. I think the issue would be that this would not force the user to remove the item or make a change. Code can delete the item from the cell but the probability that the calculate event will run again has to be dealt with. Hopefully I took care of that.

Open the vb editor (alt+F11) double click on the proper sheet from the objects list.
1722012765199.png

Choose Worksheet from the top left menu
1722012797287.png


Choose Calculate from the right menu. Paste this in between the Sub / End Sub lines:
1722012848765.png


VBA Code:
If UCase(ActiveCell.Offset(0, 4)) = "X" Then
    MsgBox "Sorry, this item is not currently available." & vbCrLf & "Please choose a different item."
    Application.EnableEvents = False
    ActiveCell = ""
    Application.EnableEvents = True
End If
See if that works for you. I suggest you do this in a copy of your workbook to test first.
You could put your message(s) in a helper sheet and pull them from there, otherwise you'll have to edit the message in the code itself.

EDIT - I corrected offset to 4.
 
Upvote 0
Solution
Thanks that is working like I want it to work. Thanks so much, I was pulling out my hair trying to figure out a way using data validation.
 
Upvote 0
Micron,
The code you gave me works great, however I have multiple sheets in my workbook and it is executing on all the sheets even though I double clicked on the sheet I want it to work for and entered the VBA code there. But now when I am on another sheet in the same workbook, it is executing on that sheet as well. I added another couple of lines in your code, but I do not think that is causing the problem. Here is the code I entered. Any suggestions?

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

End Sub
--------------------------------------------------------------------------------------------------------------------------------------------------------------------

Private Sub Worksheet_Calculate()
If UCase(ActiveCell.Offset(0, 7)) = "X" Then
MsgBox "Sorry, this item is currently unavailable in this style and color." & vbCrLf & "Please make a different selection."
Application.EnableEvents = False
ActiveCell = ""
ActiveCell.Offset(0, 1) = ""
Application.EnableEvents = True
End If
End Sub
 
Upvote 0
The only way I can see it acting on all sheets is if you pasted the code in all the affected sheets or used the event at the workbook level (SheetCalculate). Based in what you posted neither of those things appear to be the case. Try removing your edit(s) and see what happens. Perhaps this
ActiveCell.Offset(0, 1) = ""

is triggering other code to run? Or are you saying that on the other affected sheets, the exact same changes are being made? You could first try putting a break point at the 1st code line and step through (F8) and watch what happens.

Did you get my PM?
 
Upvote 0

Forum statistics

Threads
1,223,862
Messages
6,175,046
Members
452,606
Latest member
jkondrat14

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top