我必须缺少明显的东西,但我以某种方式得到类型不匹配错误。
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
编辑:
删除了稳定名称和ICOLUMN,并进行了硬编码,并继续遇到相同的错误。因此,100%确保它与列表框有关。不知道它是否有任何帮助,但是从Excel中的用户形式运行此代码。
我假设您的listbox是ActiveX listbox。如果这是正确的,则需要如下更改子上的签名:
Sub populateListBox(sTableName As String, iColumn As Integer, lb As MSForms.ListBox)
请记住,ListBox,ActiveX和表单有两种类型。ActiveX ListBox被声明为MSForms.ListBox
,而表单列表框则声明为ListBox
。