VBA 如何通过名称访问列表框 excel-2010

VBA How to access a listbox by name excel-2010

下面是我的 VBA 代码,用于从列表框中获取所选项目。我想将列表框的名称作为字符串发送,然后以这种方式访问​​列表框,因为我无法直接访问列表框,因为此代码位于工作表源代码的单独模块中。

所以我想问的是如何通过字符串访问列表框。 例如,而不是

Worksheets("sheet1").ListBox1

我想要

Worksheets("sheet1")."ListBox1"

所以我只要有列表框的名字就可以重用这个函数

下面的代码

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

提前致谢!!!

您可以使用 Worksheets("sheet1").OLEObjects("ListBox1").Object