Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA类型不匹配(13)_Excel_Typing_Vba - Fatal编程技术网

Excel VBA类型不匹配(13)

Excel VBA类型不匹配(13),excel,typing,vba,Excel,Typing,Vba,我在用户表单上有两个选项按钮(SecurityRadio和SafteyRadio)。当我点击Security radio时,我想对工作表名称“Security”应用一个过滤器,然后将该新范围分配给一个变量名。对于SwitchesRadio,我希望使用不同的工作表执行相同的过程 但是,当我选择SwitchesRadio并单击Go按钮时,当我尝试将新范围分配给其他用户窗体上的列表框时,会出现代码错误。该错误是运行时错误13。ListBox2.上的“类型不匹配”。。接近尾声(见评论)。你知道我该怎么解

我在用户表单上有两个选项按钮(SecurityRadio和SafteyRadio)。当我点击Security radio时,我想对工作表名称“Security”应用一个过滤器,然后将该新范围分配给一个变量名。对于SwitchesRadio,我希望使用不同的工作表执行相同的过程

但是,当我选择SwitchesRadio并单击Go按钮时,当我尝试将新范围分配给其他用户窗体上的列表框时,会出现代码错误。该错误是运行时错误13。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

您可能希望将
rau Range.Address
分配给
RowSource
,因为
Range
是一个对象

下面是一个非常简单的RowSource用法示例

有关范围属性的信息(中间向下)

成功了!非常感谢。我能够克服这个错误,但是当我查看我的列表框时,我可以选择区域,但是没有单词出现。是否.Address与省略这些词有冲突?抱歉,看起来像是我对如何使用RowSource;的误解。Address返回包含范围地址的字符串。再多做一点调查就会发现,这似乎和你遇到的问题一样,可能会给你带来启发。RowSource似乎需要完整的工作表名称和范围,但没有一个字符串hackery无法修复。。我会更新我的答案,如果它为您修复-没有什么比一个完整的解决方案!