VBA pop message box based on certain criteria

jimmisavage

Board Regular
Joined
Jun 28, 2017
Messages
130
I have a userform where I want to make sure the user is inputting the necessary data.

ComboBox41 only needs to be completed IF ComboBox1 has data and ComboBox21 is empty.

So if ComboBox1 is empty we can move on.
Or if ComboBox1 has data and ComboBox21 has data we can move on.
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Code:
if trim(combobox41.text)="" or ( trim(combobox41.text)<>"" and trim(combobox21.text)<>"") then
'do stuff
endif
 
Upvote 0
Hi Roderick,
This works to a degree but it still wants me to add a value to ComboBox41 when ComboBox1 is empty. It should let me continue if ComboBox1 is empty.
 
Upvote 0
Actually my example is a simplified version. This is repeated 20 times on the same form but with the next box in the sequence.
So i'm doing something like this, 20 times...

Code:
If Trim(ComboBox41.Text) = "" Or (Trim(ComboBox41.Text) <> "" And Trim(ComboBox21.Text) <> "") Then
MsgBox "Enter a default value"
Exit Sub
End If


If Trim(ComboBox42.Text) = "" Or (Trim(ComboBox42.Text) <> "" And Trim(ComboBox22.Text) <> "") Then
MsgBox "Enter a default value"
Exit Sub
End If


If Trim(ComboBox43.Text) = "" Or (Trim(ComboBox43.Text) <> "" And Trim(ComboBox23.Text) <> "") Then
MsgBox "Enter a default value"
Exit Sub
End If


If Trim(ComboBox44.Text) = "" Or (Trim(ComboBox44.Text) <> "" And Trim(ComboBox24.Text) <> "") Then
MsgBox "Enter a default value"
Exit Sub
End If
 
Last edited:
Upvote 0
Why not accumulate the potential missing data and only advise the user once?
Code:
If Trim(ComboBox41.Text) = "" Or (Trim(ComboBox41.Text) <> "" And Trim(ComboBox21.Text) <> "") Then
errsmsg = errmsg & vbcr & "Enter value in...<name of place to enter>"
End If
If Trim(ComboBox42.Text) = "" Or (Trim(ComboBox42.Text) <> "" And Trim(ComboBox22.Text) <> "") Then
errsmsg = errmsg & vbcr & "Enter value in...<name of place to enter>"
End If
If Trim(ComboBox43.Text) = "" Or (Trim(ComboBox43.Text) <> "" And Trim(ComboBox23.Text) <> "") Then
errsmsg = errmsg & vbcr & "Enter value in...<name of place to enter>"
End If
If Trim(ComboBox44.Text) = "" Or (Trim(ComboBox44.Text) <> "" And Trim(ComboBox24.Text) <> "") Then
errsmsg = errmsg & vbcr & "Enter value in...<name of place to enter>"
End If

'one time errmsg check
if trim(errmsg)<>"" then
msgbox "Input missing data:" & vbcr errmsg, vbcritical,"ALERT"
exit sub
endif
 
Upvote 0
I don't think that would really work for my purpose.
I really only need the rule to apply if ComboBox1 (followed by 2, 3 and 4 in the above example) has data in it. 1 & 2 might have data, which would then need the rule but 3 and 4 could be empty so the rule wouldn't be needed.
 
Upvote 0
Does anyone have any other ideas on how this might work? I believe we're close but it's missing one step in the chain
 
Upvote 0
So to simplify, you want to make sure all boxes have something in them? If so, then perhaps

Code:
for x = 1 to 50 'assumes 50 comboboxes
if trim(userfor1.controls("ComboBox" & x))="" then
errmsg = errmsg & vbcr "populate " & x & " box"
endif
next x
if errmsg <>"" then
msgbox "Input missing data in following boxes:" & vbcr & errmsg, vbcritical,"ALERT"
exit sub
endif
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,301
Members
452,633
Latest member
DougMo

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