Selecting Multiple Options in Dependent Drop Down List Box using VBA

In our earlier blog we have already discussed how to create a dependent drop down list box in Excel and as well as in VBA when we are using the list box in VBA.

Now in this blog, we will discuss how to select multiple options from the dependent drop down list Box as it helps to send out the customized message or perform an action based on multiple selections made by users.

If you have not gone through the blog, then please click on below link to read it with download file for working.

downloads

Hope you have the same file which we have used earlier.

Now let’s start steps by step to create a Dependent drop down list box:

We are creating a Dependent list box with multiple options (items). From the below user form we will select the 2 values (items), to get a custom message.

Dependent List Fruits Picture-1

(Picture 1)

It is very easy to select the value, but to use the same value to send a message or call for an action is not very easy. For example, We want to show the value in the message box like “I like Apple and Guava

To do the same we are using a command button (OK) in the form. Now our form looks like this:

Dependent List Blank Picture-2

(Picture 2)

So if you write the below code like we use for selecting the value it will not work.

Coding Picture-3

(Picture 3)


Now we have written the code and try to run the code. But we got only “I like” in the message box (It’s a problem).

Dependent List I Like Picture-4

(Picture 4)

So now we have to change the code to get the selected value in the message box by these steps:

  1. Double Click on “OK” button.
  2. Paste the below code
  3. Run the Form
  4. Select values from the dependent drop down list
  5. Then Press OK button

Here is the code:

Private Sub CmdOk_Click()

Dim First As Boolean

Dim i As Long

Dim List As String

First = True

With ListBox1

For i = 0 To .ListCount - 1

If .Selected(i) Then

If First Then

List = .List(i)

First = False

Else

List = List & "," & .List(i)

End If

End If

Next i

End With

MsgBox "I like " & List, vbOKOnly, "Fruits"

End Sub

Code will like below image:

Coding Picture-5

(Picture 5)

Related blog on: How to split Excel data into separate sheets using VBA?

Now let’s run the code and then select the values from the dependent list and then press “OK” button.

Dependent List I Like Apple&Guava Picture-6

(Picture 6)

As we have selected “Apple” and “Guava” and we are getting the same value in the msg box. Now If we choose any values from the dependent list and then press the “OK” button, then it will only show the value which we will select.

We found one bug (problem) in this code. The bug is that if we don’t choose any value from the dependent list and press “OK” button then it will show the below message.

Dependent List Without Selecting Picture-7

(Picture 7)

So we need to apply another data validation for multiple selections over here so that if we don’t select any value from the dependent list box because it will give us the message that we must select a value from the dependent list. We need to change the code.

Here is the revised code:

Private Sub CmdOk_Click()

Dim First As Boolean

Dim i As Long

Dim List As String

First = True

With ListBox1

For i = 0 To .ListCount - 1

If .Selected(i) Then

If First Then

List = .List(i)

First = False

Else

List = List & "," & .List(i)

End If

End If

Next i

End With

If List = "" Then

MsgBox "You must select an item!!!", vbExclamation, "Item"

Exit Sub

End If

MsgBox "I like " & List, vbOKOnly, "Fruits"

End Sub

This time, our code looks like this:

Coding Picture-8

(Picture 8)

Now if we don’t select any value then, this time, we will get a message that we need to select at least one value from the list box:

Dependent List Error Picture-9

(Picture 9)

We can also use another value also like this way. Just give the same name for all the boxes we have used in the form and then use the same code in the list box.

Finally, we can guarantee that you will enjoy preparing such kind of reports using VBA with our advanced Excel VBA tutorials.

You can also check out our: FREE Excel VBA Tutorial

Related Tutorials

Delete Duplicate in Excel or Remove Duplicate in Excel
November 9, 2018
Excel Formulas PDF
September 6, 2018
How To Lock Cells in Excel | Unprotect Excel
August 13, 2018
4x Faster at Excel
August 6, 2018
Separate Content of One Excel Cells into Separate Columns
August 3, 2018
How to Transpose Excel Columns to Rows | Paste Special Method
July 26, 2018
How to create sparklines in Excel
July 19, 2018
AutoSum in Excel with Shortcut
July 17, 2018
OFFSET Function in Excel
July 6, 2018
Strikethrough Shortcut in Excel & Word
July 4, 2018
INDIRECT Function with SUM, MAX, MIN & Independent Cell Value
June 29, 2018
Pivot Table Slicers In Excel
June 12, 2018
How to Split Cells in Excel using Text to Column
June 7, 2018
How to Wrap Text in Excel Automatically and Manually
June 6, 2018
How to Hide/Unhide Column in Excel
June 5, 2018
Highlight row based on cell value
June 4, 2018
Learn how to remove blank cells in Excel
June 3, 2018
How to Group Numbers, Dates & Text in Pivot table in Excel
June 1, 2018
5 Powerful Tricks to Format cells in Excel
May 31, 2018
Insert a Picture into a Cell in Excel
May 25, 2018
What is ISFORMULA Function and FORMULATEXT Function
May 21, 2018
How to Use SUBSTITUTE Function
May 21, 2018
Excel Quartile Function in Excel
May 8, 2018
How to use the Excel PERCENTILE function
May 7, 2018
Insert or Type degree symbol in Excel with Autocorrect Feature
May 7, 2018
25% Discount
No prize
All Courses at $200
Almost!
10% Discount
Free Ebook
No Prize
No luck today
Almost!
50% Discount
No prize
80% Discount
Get a chance to boost your knowledge!
Use this coupon code for any course that you wish for. 
Our in-house rules:
  • You can choose any course & redeem coupon
  • If you find any difficulty, mail us on [email protected]
  • Wheel Spin will end soon
  • Coupon code can be applied within 2 days.