Excel VBA : Adding Drop-Down List to ComboBox

RAKESH230583

New Member
Joined
Jan 10, 2011
Messages
46
Hi,

I have one Excel Userform, Under which I have 7 Different comboBox and for source for the every single combobox I had used the below quoted VBA under Private Sub UserForm_Activate()

Under Sheet2("Source") - under column range A:G - I had listed all the value which I required to show under each combobox.

Below quoted VBA code works fine for me - But Is there any possibility that instead of using such big and complicated code I can use the simple and short code which will give me the same results ?

Code:
Private Sub UserForm_Activate()

Dim Ws As Worksheet
Dim i As Integer
Set Ws = ThisWorkbook.Sheets("Source")

Me.ComboBox1.Clear
For i = 2 To Ws.Range("A" & Application.Rows.Count).End(xlUp).Row
Me.ComboBox1.AddItem Ws.Range("A" & i).Value
Next I

Me.ComboBox2.Clear
For i = 2 To Ws.Range("B" & Application.Rows.Count).End(xlUp).Row
Me.ComboBox2.AddItem Ws.Range("B" & i).Value
Next I

Me.ComboBox3.Clear
For i = 2 To Ws.Range("C" & Application.Rows.Count).End(xlUp).Row
Me.ComboBox3.AddItem Ws.Range("C" & i).Value
Next I

Me.ComboBox4.Clear
For i = 2 To Ws.Range("D" & Application.Rows.Count).End(xlUp).Row
Me.ComboBox4.AddItem Ws.Range("D" & i).Value
Next I

Me.ComboBox5.Clear
For i = 2 To Ws.Range("E" & Application.Rows.Count).End(xlUp).Row
Me.ComboBox5.AddItem Ws.Range("E" & i).Value
Next I

Me.ComboBox6.Clear
For i = 2 To Ws.Range("F" & Application.Rows.Count).End(xlUp).Row
Me.ComboBox6.AddItem Ws.Range("F" & i).Value
Next I

Me.ComboBox7.Clear
For i = 2 To Ws.Range("G" & Application.Rows.Count).End(xlUp).Row
Me.ComboBox7.AddItem Ws.Range("G" & i).Value
Next I

Me.Date_Picker.Value = Date

End Sub
 
Last edited by a moderator:

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Since your combo names and column numbers correspond, you can use a simple loop:

Code:
Private Sub UserForm_Activate()

Dim Ws As Worksheet
Dim i As Long
Dim n as long
Set Ws = ThisWorkbook.Sheets("Source")

For n = 1 to 7
Me.Controls("ComboBox" & n).List = Ws.Range(ws.Cells(2, n), ws.cells(ws.Rows.Count, n).End(xlUp)).Value
Next

Me.Date_Picker.Value = Date

End Sub
 
Upvote 0
I have used the above suggested vba code - and able to get the correct combobox list too.

Can anyone please tell me how can i validate the said list, as i dont want user to manual type the data which in not under the source list.

And if any user try to manually input details which is not under the source list - Then they should get the error message "Incorrect selection - Kindly select the data from the list available or request to update the source list"
 
Upvote 0
Just change the combobox's Style property to 2 and the users will not be able to type anything that isn't in the list.
 
Upvote 0

Forum statistics

Threads
1,214,988
Messages
6,122,620
Members
449,092
Latest member
amyap

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