VBA ListBox - get the value of the selected item and dislay it in a message box

FaheemAhmadGul
FaheemAhmadGul used Ask the Experts™
on
I am displaying a few strings items on ListBox on a Form. I would like to display the value of the item (which is a string) displayed in a message box when I click on it. I would appreciate your help.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Analyst Assistant
Commented:
Perhaps.
Private Sub ListBox1_Click()
    MsgBox ListBox1.Value
End Sub

Open in new window

Author

Commented:
Thank you for your comment. This does not work, however. We need the value of the selected item and not of the ListBox - that is we want to display the value of the item on which the user clicks. This will involve finding out which item the user clicked on and then fidning its value.
NorieAnalyst Assistant

Commented:
That code should show you the item that's been clicked in the listbox because Value returns the value of the selected item in the listbox.
GrahamSkanRetired
Top Expert 2012
Commented:
Try it like this:
Private Sub ListBox1_Change()
    MsgBox ListBox1.List(ListBox1.ListIndex)
End Sub

Open in new window

Author

Commented:
Many thanks. Actually both the solutions work. I have split the points - more points to Norie because that solution was post first. Hope this OK.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial