VBA How to access a listbox by name excel-2010

瘦欲@ 提交于 2020-01-16 04:30:12

问题


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!!!


回答1:


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



来源:https://stackoverflow.com/questions/51771719/vba-how-to-access-a-listbox-by-name-excel-2010

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!