Creating a drop down list box in Excel (a beginner)
I am a beginner of VBA. I searched the forum for the answer of my question but I have a hard time understanding the terminology in the posts.
Basically, I just want to create a drop down list box in VBA. The topics that will appear in the list box are already fixed and will never ever change in the future. The user will pick a topic from the list and the code will take appropriate action accordingly. Can someone tell me how to do this in layman's terms? Or could you lead me to a reading?? I have been googling about this but couldn't find a tutorial.
Thanks!
Re: Creating a drop down list box in Excel (a beginner)
if it is a fixed list you can add the items to the listbox at design rather than in code, then just use code to do whatever depending on which item is selected
Re: Creating a drop down list box in Excel (a beginner)
Why are you posting the same question again and again? :mad:
Pradeep
Re: Creating a drop down list box in Excel (a beginner)
westconn1, thanks for your response. How can I add items to the listbox in "design rather than code". Thanks in advance!!
Re: Creating a drop down list box in Excel (a beginner)
what program are you using listbox?
and what version?
Re: Creating a drop down list box in Excel (a beginner)
do you want to create the listbox / combobox on an excel worksheet or create it on a form?
if its on a worksheet
http://www.techonthenet.com/excel/qu...eate_combo.php
otherwise,
create a form within VBA name it form1 or what ever you want,
create a combo box on the form
name it:
listBox1 or what ever you want
Code:
Private Sub form1_Activate()
listBox1.AddItem "Item1"
listBox1.AddItem "Item2"
listBox1.AddItem "Item3" 'etc
End Sub
this will run when the user changes the combo box
Code:
Private Sub listBox1_Change()
Dim tempValue As String
tempValue=listBox1.Value
If tempValue = "item1" Then
'do this
ElseIf tempValue = "item2" Then
'do this
Else
'do this
End If
End Sub
Hope this helps!
Re: Creating a drop down list box in Excel (a beginner)
Hi I was just wondering if you need to create that in a worksheet then there is a simple way of doing it rather than using VB.
Use Excel Validation
Steps
1) In any Column, type the name of the topics. Say in cell A1, A2, A3, A4, A5 type "apples", "oranges", "grapes", "guava" and "Melon" respectively
2) select the cell where you want the list to populate say B1
3)click on the menu data->validation
4)Under the Settings tab select "lists" as the validation criteria
5) click the little red arrow in the source "prompt"
6) select the range i.e A1:A5 and press enter twice
your list is ready! if you click on B1, you will see your list will drop down :-)
Re: Creating a drop down list box in Excel (a beginner)
that's a good idea koolsid :)
I have a problem though, I have created a drop down list, however, it refers to a non-unique list, so I have duplicate options. It does not look really good, especially if I have a lot of multiple similar options. Due to the nature of the data, I can not force the source of entries only contain unique data.
So, I am looking for a solution to generate a unique validation list (ie. neglecting duplicate entries). Any ideas?
Thanks.
Re: Creating a drop down list box in Excel (a beginner)
If you are refering to creating the list in Excel sheet then the best way to do it is create a pivot table (check the link in my signature) and then use that list for the validation... this way you will always get one entry rather then duplicate entries...
In the layout part of the pivot table, keep the say 'names' field in the row and the 'count of names' in the data.
Hope this helps...