VBA – 无法填充列表框,运行时 13 类型不匹配

VBA – 无法填充列表框,运行时 13 类型不匹配

问题:以下代码在调用此函数时出现运行时错误 13 类型不匹配。不知道为什么……?

Option Explicit
Option Compare Text

Private Const headerSelected1 = 1
Private Const headerSelected2 = 4
Private Const headerSelected3 = 9
Private Const headerSelected4 = 14
Private Const headerSelected5 = 20
Private Const headerSelected6 = 31
Private Const headerSelected7 = 44
Private Const headerSelected8 = 57
Private Const headerSelected9 = 158


Private Sub UserForm_Initialize()
        Call PopulateListboxData
        UserformReports.ListBoxData.Selected(0) = True
        UserformReports.TextBoxWord1.SetFocus
        Me.LabelReportsMessage = vbNullString
End Sub

Private Function PopulateListboxData()
            Me.LabelReportsMessage = "Please wait . .  . "
            Dim rowi As Long
            Dim arrList As Variant
            Dim ws As Worksheet
            Dim itemCell
            Dim lastRow As Long

            Set ws = Worksheets("Database")

            ListBoxData.Clear
            ListBoxData.ColumnCount = 199
            lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row

            Me.LabelSearch1 = ws.Cells(8, headerSelected1).Value
            Me.LabelSearch2 = ws.Cells(8, headerSelected2).Value     ''''''<<<< Run-time error 13 type mismatch.
            Me.LabelSearch3 = ws.Cells(8, headerSelected3).Value
            Me.LabelSearch4 = ws.Cells(8, headerSelected4).Value

            For rowi = 1 To lastRow
               If ws.Cells(rowi, headerSelected1).Value <> vbNullString Then ListBoxData.AddItem ws.Cells(rowi, headerSelected1).Value
               If ws.Cells(rowi, headerSelected2).Value <> vbNullString Then ListBoxData.List(rowi - 1, 1) = ws.Cells(rowi, headerSelected2).Value
               If ws.Cells(rowi, headerSelected3).Value <> vbNullString Then ListBoxData.List(rowi - 1, 2) = ws.Cells(rowi, headerSelected3).Value
               If ws.Cells(rowi, headerSelected4).Value <> vbNullString Then ListBoxData.List(rowi - 1, 3) = ws.Cells(rowi, headerSelected4).Value
               If ws.Cells(rowi, headerSelected5).Value <> vbNullString Then ListBoxData.List(rowi - 1, 4) = ws.Cells(rowi, headerSelected5).Value
               If ws.Cells(rowi, headerSelected6).Value <> vbNullString Then ListBoxData.List(rowi - 1, 5) = ws.Cells(rowi, headerSelected6).Value
               If ws.Cells(rowi, headerSelected7).Value <> vbNullString Then ListBoxData.List(rowi - 1, 6) = ws.Cells(rowi, headerSelected7).Value
               If ws.Cells(rowi, headerSelected8).Value <> vbNullString Then ListBoxData.List(rowi - 1, 7) = ws.Cells(rowi, headerSelected8).Value
               If ws.Cells(rowi, headerSelected9).Value <> vbNullString Then ListBoxData.List(rowi - 1, 8) = ws.Cells(rowi, headerSelected9).Value
            Next rowi
    End Function

相关内容