【发布时间】:2017-06-05 06:47:25
【问题描述】:
我是 VBA 新手,我一直在使用一段代码来排序、删除重复项并在我的工作表的某个范围内填充 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(组合框)和RngBeg(范围)。使用FillComboFromRange cboTask, Wks.Range("b4")之类的方式调用该 Sub