2
我在用戶窗體上有兩個選項按鈕(SecurityRadio和SafteyRadio)。當我點擊安全收音機時,我想將過濾器應用於工作表名稱「安全」,然後將該新範圍分配給變量名稱。對於SwitchesRadio,我希望執行相同的過程,但使用不同的工作表。VBA類型不匹配(13)
但是,當我選擇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
工作!非常感謝。 – Liz
我能夠通過這個錯誤,但是當我看着我的列表框時,我可以選擇區域,但沒有出現任何單詞。地址與拉扯單詞有衝突嗎? – Liz
對不起,看起來像我對如何使用RowSource的誤解; .Address返回一個包含範圍地址的字符串。多一點調查顯示http://stackoverflow.com/a/30933151/4541045似乎是你遇到的同樣的麻煩,可能是啓發。看起來RowSource期待完整的工作表名稱和範圍,但沒有什麼是一個小小的字符串hackery無法修復的。我會更新我的答案,如果它能夠爲您解決它 - 完全沒有什麼解決方案! – ti7