我想用来自2个不同表单的数据填充组合框。
我具有创建2个有效范围的功能:
Dim lst as Range
Dim lst2 as Range
Set sht1 = ThisWorkbook.Worksheets("BaseAliments")
Set sht2 = ThisWorkbook.Worksheets("BaseRecettes")
Set lst = Range(sht1.Range("A1").address, sht1.Range("A" & sht1.Rows.Count).End(xlUp).address)
Set lst2 = Range(sht2.Range("A1").address, sht2.Range("A" & sht2.Rows.Count).End(xlUp).address)
但是当我尝试填充listfillrange时:
Set Ctrl =Worksheets("Menu").OLEObjects.Add(ClassType:="Forms.ComboBox.1")
With Ctrl
.Name = "CB"
'.ListFillRange = 'Something to concatenate lst and lst2
End With
答案 0 :(得分:2)
with open("snnifflog.txt") as f:
lines = f.read().splitlines()
timeindex = [i for i in range(len(lines)) if lines[i].startswith("2019")]
nospaces = [s.replace(" ", "") if i not in timeindex else s for i, s in enumerate(lines)]
messages = [nospaces[timeindex[i] + 1: timeindex[i + 1]] for i in range(len(timeindex) - 2)]
根本无法在不同的工作表上使用。我认为您最好的选择是单独添加以下项目:
Union
答案 1 :(得分:1)
我高度推荐一种使用For-Each
的方法,就像Jvdv所展示的那样,因为它简单,直观且优雅。
但是,如果您绝对必须按照自己的方式进行操作(将两个列表合并为一个),那么这是我的建议:
Sub initializeCombo()
Dim sht1 As Worksheet
Dim sht2 As Worksheet
Dim sht3 As Worksheet
Dim rng1 As Range
Dim rng2 As Range
Dim lst1 As Variant
Dim lst2 As Variant
Dim lst3 As Variant
Dim i As Long
Set sht1 = ThisWorkbook.Worksheets("WS1") 'The worksheet where the first list is
Set sht2 = ThisWorkbook.Worksheets("WS2") 'The worksheet where the second list is
Set sht3 = ThisWorkbook.Worksheets("WS3") 'The worksheet where the ComboBoxt is
Set rng1 = sht1.Range(sht1.Range("A1"), sht1.Range("A" & sht1.Rows.Count).End(xlUp))
Set rng2 = sht2.Range(sht2.Range("A1"), sht2.Range("A" & sht2.Rows.Count).End(xlUp))
lst1 = rng1
lst2 = rng2
''''''''''''Combine the 2 lists into one'''''''''''''
ReDim lst3(1 To UBound(lst1) + UBound(lst2)) '
For i = LBound(lst1) To UBound(lst1) Step 1 '
lst3(i) = lst1(i, 1) '
Next i '
For i = UBound(lst1) + 1 To UBound(lst3) Step 1 '
lst3(i) = lst2(i - UBound(lst1), 1) '
Next i '
'''''''''''''''''''''''''''''''''''''''''''''''''''''
sht3.OLEObjects("ComboBox1").Object.List() = lst3
End Sub
最后,如果您想以编程方式在工作表sht3中创建一个ActiveX
组合框,并为其分配lst3
,则必须这样做:
Dim Ctrl As Object
Set Ctrl = sht3.OLEObjects.Add(ClassType:="Forms.ComboBox.1").Object
Ctrl.List() = lst3
更新
经修改可与ActiveX组合框一起使用的JvdV版本如下:
Sub initializeCombo2()
Dim sht1 As Worksheet, sht2 As Worksheet, sht3 As Worksheet
Dim lst1 As Range, lst2 As Range, cell As Range
Dim Ctrl As Object
Set sht1 = ThisWorkbook.Worksheets("BaseAliments") 'The worksheet where the first list is
Set sht2 = ThisWorkbook.Worksheets("BaseRecettes") 'The worksheet where the second list is
Set sht3 = ThisWorkbook.Worksheets("Menu") 'The worksheet where the ComboBoxt is
Set lst1 = sht1.Range(sht1.Range("A1"), sht1.Range("A" & sht1.Rows.Count).End(xlUp))
Set lst2 = sht2.Range(sht2.Range("A1"), sht2.Range("A" & sht2.Rows.Count).End(xlUp))
Set Ctrl = sht3.OLEObjects.Add(ClassType:="Forms.ComboBox.1").Object
For Each cell In lst1
Ctrl.AddItem cell.Value
Next cell
For Each cell In lst2
Ctrl.AddItem cell.Value
Next cell
End Sub