VBA for If one value is selected in a combo box ensure another combo box is completed

Lesgrimes

New Member
Joined
Oct 12, 2011
Messages
5
Hi, I'm very new to VBA and was wondering if somebody could help me with the following

I have a user form with multiple combo boxes and text boxes.

One of these combo boxes has three options, all entered via VBA

Private Sub ComboBoxCategory_DropButt*******()
'Populate control.
Me.ComboBoxCategory.AddItem "Beverage"
Me.ComboBoxCategory.AddItem "Food"
Me.ComboBoxCategory.AddItem "Hotel Supplies"
End Sub

If the option of Hotel Supplies is selected (and only this option), I want to ensure that a different combo box has one of its options selected.

I would like a message box to appear at time of selection not at the end when the command button for Save is clicked (I have a check for empty fields for everything else at this point)

Is this possible and if so, how do I go about creating the code for this?

Thanking you in advance for reading and hopefully replying
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Try this

Code:
Dim qClose As Boolean


Private Sub ComboBoxCategory_Change()
  If ComboBoxCategory.Value = "Hotel Supplies" Then
    If ComboBoxDifferent.ListIndex = -1 Then
      MsgBox "You must select an option en combo2"
      ComboBoxDifferent.SetFocus
    End If
  End If
End Sub


Private Sub ComboBoxDifferent_Exit(ByVal Cancel As MSForms.ReturnBoolean)
  If qClose Then Exit Sub
  If ComboBoxCategory.Value = "Hotel Supplies" Then
    If ComboBoxDifferent.Value = "" Or ComboBoxDifferent.ListIndex = -1 Then
      MsgBox "You must select an option"
      Cancel = True
    End If
  End If
End Sub


Private Sub UserForm_Activate()
  Me.ComboBoxCategory.AddItem "Beverage"
  Me.ComboBoxCategory.AddItem "Food"
  Me.ComboBoxCategory.AddItem "Hotel Supplies"
  
  ComboBoxDifferent.AddItem "option1"
  ComboBoxDifferent.AddItem "option2"
  ComboBoxDifferent.AddItem "option3"
  
  qClose = False
End Sub


Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
  qClose = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,755
Messages
6,180,758
Members
452,996
Latest member
nelsonsix66

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