gpt4 book ai didi

excel - VBA 类型不匹配 (13)

转载 作者:行者123 更新时间:2023-12-04 20:59:58 34 4
gpt4 key购买 nike

我在用户表单上有两个选项按钮(SecurityRadio 和 SafteyRadio)。当我单击安全单选时,我想将过滤器应用于工作表名称“安全”,然后将该新范围分配给变量名称。对于 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

最佳答案

您可能想要分配 RA_Range.AddressRowSource作为 Range是一个对象。

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

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

关于excel - VBA 类型不匹配 (13),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38249806/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com