PDA

View Full Version : Solved: List Box in Excel VBA



sarah2010
05-11-2011, 08:41 AM
Hello,
I have created a form in Ecel VBA & one of the components of the form is a ListBox. I want the list box to have 3 items, say "Test1, Test2, Test3". Based on the option I choose from the list, I need a message to be displayed saying, "you selected Test xx".
I played in VB.net & figured it out. However, I don't see that similar in Excel VBA. Any help is appreciated, thanks!

Bob Phillips
05-11-2011, 09:25 AM
Select Case ListBox1.Value

Case "Test1": MsgBox "You selected Test 1"
Case "Test2": MsgBox "You selected Test 2"
Case "Test3": MsgBox "You selected Test 3"
End Select

sarah2010
05-11-2011, 11:02 AM
Thanks for the reply XLD, however, I'm not able to make it work. Here's what I tried. Let me know the missing piece. Thanks!

Bob Phillips
05-11-2011, 11:24 AM
That code should be in the form's ListBox1_Click event, and you need to load the listbox.

sarah2010
05-11-2011, 11:58 AM
Still, no luck, attached is my newest version. thanks!

Bob Phillips
05-11-2011, 12:09 PM
As I said, you need to load the listbox with data, otherwise you have nothing to select.

sarah2010
05-11-2011, 12:16 PM
can you brief on loading the listbox with data, I've no idea on how to do that in VBA. In vb.net, I feed the items in the list under the "property --> items" field. However in VBA, I don't see anything like that. thanks!

Bob Phillips
05-11-2011, 12:31 PM
Private Sub UserForm_Initialize()
With Me.ListBox1

.AddItem "Test1"
.AddItem "Test2"
.AddItem "Test3"
End With
End Sub

sarah2010
05-12-2011, 06:26 AM
Thanks much for the detailed instructions! worked great!