我一定遗漏了一些明显的东西,但不知何故我得到了一个类型不匹配的错误。
Private Sub btnGenerate_Click()
populateListBox "Table_Install_Base", 1, Me.lbPressType
end sub
Sub populateListBox(sTableName As String, iColumn As Integer, lb As ListBox)
Dim v, e
With Sheets(1).ListObjects(sTableName).ListColumns(iColumn).DataBodyRange
v = .Value
End With
With CreateObject("scripting.dictionary")
.comparemode = 1
For Each e In v
If Not .exists(e) Then .Add e, Nothing
Next
If .Count Then lb.List = Application.Transpose(.keys)
End With
end sub
编辑:
删除了 sTableName 和 iColumn 并对这些值进行了硬编码,仍然出现相同的错误。所以 100% 确定它与列表框有关。不知道它是否有任何帮助,但从 Excel 的用户窗体中运行此代码。
最佳答案
我假设您的 ListBox 是 ActiveX ListBox。如果这是正确的,您需要更改 Sub 上的签名,如下所示:
Sub populateListBox(sTableName As String, iColumn As Integer, lb As MSForms.ListBox)
请记住,有两种类型的 ListBox,ActiveX 和 Forms。 ActiveX 列表框被声明为
MSForms.ListBox
而表单列表框被声明为 ListBox
.
关于vba - 在 VBA 中将列表框作为函数参数传递给我一个运行时错误 13,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48949534/