1

Below is my VBA code to get a selected item from a listbox. I want to send the name of the listbox as a string then access the list box that way since I can't access the listbox directly because this code is in a seperate module from my worksheet source code.

So what I am asking is how do you access a listbox by string. For example, instead of

Worksheets("sheet1").ListBox1

I want something like

Worksheets("sheet1")."ListBox1"

So I can reuse this function as long as I have the name of the listbox

Code below

Public Function getListBoxSelection(listBox As MSForms.listBox) As String
    Dim colCount As Integer
    Dim I As Long
    Dim selectedItem As String

    If listBox.ListIndex <> -1 Then
        For I = 0 To (listBox.ColumnCount - 1)
             selectedItem = selectedItem & listBox.column(I)
        Next I
    End If
    getListBoxSelection = selectedItem
End Function

Thanks in advance!!!

User
  • 363
  • 7
  • 24

1 Answers1

4

You can use Worksheets("sheet1").OLEObjects("ListBox1").Object

Josh Eller
  • 2,045
  • 6
  • 11