下面是我的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提前感谢!
发布于 2018-08-10 00:25:59
您可以使用Worksheets("sheet1").OLEObjects("ListBox1").Object
https://stackoverflow.com/questions/51771719
复制相似问题