我是VBA的新手,已經使用一段代碼對工作表上的特定範圍進行排序,刪除重複項和填充Combobox。我的問題是,我需要添加什麼,以便可以從另一列填充另一個Combobox,並且仍然有它。代碼爲一個工作,但我怎麼能爲多個組合框工作
我使用的代碼如下。正如你所看到的,我現在正在用從B4開始的信息填充cboTask。我想添加另一個範圍來填充另一個Combobox,這將是cboEquipment,信息從D4開始。
Dim Cell As Range
Dim Col As Variant
Dim Descending As Boolean
Dim Entries As Collection
Dim Items As Variant
Dim index As Long
Dim j As Long
Dim RngBeg As Range
Dim RngEnd As Range
Dim row As Long
Dim Sorted As Boolean
Dim temp As Variant
Dim test As Variant
Dim Wks As Worksheet
Set Wks = ThisWorkbook.Worksheets("Maintenance")
Set RngBeg = Wks.Range("b4")
Col = RngBeg.Column
Set RngEnd = Wks.Cells(Rows.Count, Col).End(xlUp)
Set Entries = New Collection
ReDim Items(0)
For row = RngBeg.row To RngEnd.row
Set Cell = Wks.Cells(row, Col)
On Error Resume Next
test = Entries(Cell.Text)
If Err = 5 Then
Entries.Add index, Cell.Text
Items(index) = Cell.Text
index = index + 1
ReDim Preserve Items(index)
End If
On Error GoTo 0
Next row
index = index - 1
Descending = False
ReDim Preserve Items(index)
Do
Sorted = True
For j = 0 To index - 1
If Descending Xor StrComp(Items(j), Items(j + 1), vbTextCompare) = 1 Then
temp = Items(j + 1)
Items(j + 1) = Items(j)
Items(j) = temp
Sorted = False
End If
Next j
index = index - 1
Loop Until Sorted Or index < 1
cboTask.List = Items
預先感謝您,我認爲這將會像複製代碼和更改暗淡值一樣簡單,但它似乎不起作用。
把你的代碼放在一個帶有兩個參數'cbo'(ComboBox)和'RngBeg'(Range)的獨立子文件中。使用類似'FillComboFromRange cboTask,Wks.Range(「b4」)' –