VBA类型不匹配(13)

时间:2016-07-07 15:43:17

标签: excel-vba typing vba excel

我在用户表单上有两个选项按钮(SecurityRadio和SafteyRadio)。当我点击安全无线电时,我想将一个过滤器应用到工作表名称"安全"然后将该新范围分配给变量名称。对于SwitchesRadio,我希望发生相同的过程但使用不同的表。

然而,当我尝试将新范围分配给不同用户表单上的列表框时,当我选择SwitchesRadio并单击Go按钮时,我的代码出错。错误是运行时错误13,"类型不匹配" on .ListBox2 ..接近结尾(见评论)。知道如何解决这个问题吗?

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

1 个答案:

答案 0 :(得分:0)

您可能希望将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