VBA 类型不匹配 (13)

VBA Type Mismatch (13)

我在用户表单上有两个选项按钮(SecurityRadio 和 SafteyRadio)。当我单击“安全”单选按钮时,我想对 sheet 名称 "Security" 应用过滤器,然后将该新范围分配给变量名称。对于 SwitchesRadio,我希望发生相同的过程,但 sheet.

但是,当我 select SwitchesRadio 并单击 Go 按钮时,当我尝试将新范围分配给不同用户表单上的列表框时,我的代码出现错误。错误是 运行-Time error 13,"Type mismatch" on .ListBox2.. near the end(见评论)。知道我该如何解决这个问题吗?

Private Sub GoButton_Click()
Dim Security As Worksheet, Switches As Worksheet, CurrentSheet As Worksheet
Dim LastAddressCurrent1 As Range, LastRowCurrent1 As Long, LastColCurrent1 As Long
Dim LastAddressCurrent2 As Range, LastRowCurrent2 As Long, LastColCurrent2 As Long
Dim RA_Range As Range, Comp_Range As Range

If SwitchesRadio Then
    Set CurrentSheet = Sheets("Switches")
ElseIf SecurityRadio Then
    Set CurrentSheet = Sheets("Security")
Else
    MsgBox "Please select a product type to continue"
End If

'retrieve the last cell row number and column
With CurrentSheet
    Set LastAddressCurrent1 = .Cells(.Rows.Count, "A").End(xlUp)
    LastRowCurrent1 = LastAddressCurrent1.Row
    LastColCurrent1 = Cells(1, Columns.Count).End(xlToLeft).Column
End With

CurrentSheet.Range(Cells(2, 1), Cells(LastRowCurrent1, LastColCurrent1)).AutoFilter Field:=2, Criteria1:="RA"
    With CurrentSheet
    Set LastAddressCurrent2 = .Cells(.Rows.Count, "A").End(xlUp)
    LastRowCurrent2 = LastAddressCurrent2.Row
    LastColCurrent2 = Cells(1, Columns.Count).End(xlToLeft).Column
    End With
Set RA_Range = CurrentSheet.Range(Cells(2, 1), Cells(LastRowCurrent2, LastColCurrent2))
CurrentSheet.ShowAllData
CurrentSheet.Range(Cells(2, 1), Cells(LastRowCurrent1, LastColCurrent1)).AutoFilter Field:=2, Criteria1:="Comp"
    With CurrentSheet
    Set LastAddressCurrent2 = Cells(.Rows.Count, "A").End(xlUp)
    LastRowCurrent2 = LastAddressCurrent2.Row
    LastColCurrent2 = Cells(1, Columns.Count).End(xlToLeft).Column
    End With
Set Comp_Range = CurrentSheet.Range(Cells(2, 1), Cells(LastRowCurrent2, LastColCurrent2))
CurrentSheet.ShowAllData

'Assign names to appropriate list boxes
With MainSelectionForm
.ListBox2.RowSource = RA_Range ****** errors here 
.ListBox1.RowSource = Comp_Range

End With
End Sub

您可能想将 RA_Range.Address 分配给 RowSource,因为 Range 是一个对象。

这是一个非常简单的 RowSource 用法示例
https://msdn.microsoft.com/en-us/library/office/gg251646.aspx

关于范围属性的信息(中途)
https://msdn.microsoft.com/en-us/library/office/ff197454.aspx