Excel 2003 ListBox类型调用sub时不匹配

时间:2011-09-19 15:26:59

标签: vba

这是工作簿级别的Sub:

Public Sub BuildList(targetSheet As Worksheet, ByRef targetListBox As ListBox, lastRow As Integer)

    For r = 1 To lastRow

        If Trim(targetSheet.Range("A" & r).Value) <> "" Then

            With targetListBox

                .AddItem Trim(targetSheet.Range("A" & r).Value)
                .List(.ListCount - 1, 1) = Trim(targetSheet.Range("B" & r))

            End With

        End If

    Next r


End Sub

以下是调用它的Userform中的一些代码:

Private Sub UserForm_Initialize()

    Dim ws As Worksheet
    Dim bottomRow As Integer

    Set ws = Worksheets("Our Status Code")
    bottomRow = ws.Range("A65536").End(xlUp).Row

    ThisWorkbook.BuildList ws, StatusCodesListbox, bottomRow


End Sub

当它遇到调用BuildList的行时,它会抛出错误13而我看不清楚原因。

1 个答案:

答案 0 :(得分:1)

你需要

 ByRef targetListBox As MSForms.ListBox

Excel有两种类型的列表框。