在VBA中将列表框作为函数参数传递给我一个运行时错误13

时间:2018-02-23 14:00:02

标签: vba excel-vba excel

我必须遗漏一些显而易见的东西,但不知何故我会遇到类型不匹配的错误。

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中的用户窗体内运行此代码。

1 个答案:

答案 0 :(得分:1)

我假设您的ListBox是一个ActiveX ListBox。如果这是正确的,您需要更改Sub上的签名,如下所示:

Sub populateListBox(sTableName As String, iColumn As Integer, lb As MSForms.ListBox)

请记住,有两种类型的ListBox,ActiveX和Forms。 ActiveX ListBox声明为MSForms.ListBox,而Forms ListBox声明为ListBox

相关问题