使用数组中的两个文本字段填充组合框 [英] Populate Combobox with Two Text Fields from an Array
问题描述
我正在尝试从数组中的2个单元格中获取值,以填充用户窗体上组合框的文本字段。数组中的值如下所示:
A 1
B 2
C 3
B 4
我想在组合框文本字段中区分B2和B4。该字段当前填充为 B
。我希望它使用 B 2
代替。该问题与
填充组合框 ComboBox1
像这样:遍历数据并使用 .AddItem
添加两个数据列的组合。
Private Sub UserForm_Initialize()
Dim Data()As Variant'array
Data = Worksheets( Sheet1) .Range( A1:B4)。Value'将数据读入数组
Me.ComboBox1.Clear
Dim iRow as long
对于iRow = LBound( Data,1)到UBound(Data,1)
Me.ComboBox1.AddItem Data(iRow,1)& & Data(iRow,2)
接下来的iRow
End Sub
然后您可以选择您的项目,如下所示:
< img src = https://i.stack.imgur.com/MfhdI.png alt =在此处输入图片描述>
可以使用 Me.ComboBox1.Text
检索值 B 2
:
Debug.Print Me.ComboBox1.Text'返回B 2
I'm trying to get values from 2 cells in an array to populate the text field of a combobox on a userform. The values in the array look like this:
A 1
B 2
C 3
B 4
I would like to make a distinction between B2 and B4 in the combobox text field. The field currently populates like B
. I would like it to populate with B 2
instead. This question is partly related to this question.
I tried using this that was linked from here but couldn't get the ListCount property to work. I used this to better understand arrays in a hope that it would provide some insight. I tried this approach but it doesn't populate the text field. WhereInArray looks like it is more for finding a value and also appears to be unique-value-dependent. I tried this but it seems to get tripped up on the array. Any help would be greatly appreciated.
Imagine this data in Worksheets("Sheet1")
Populate your combobox ComboBox1
like this: Loop through the data and use .AddItem
to add a combination of both data columns.
Private Sub UserForm_Initialize()
Dim Data() As Variant 'array
Data = Worksheets("Sheet1").Range("A1:B4").Value 'read data into array
Me.ComboBox1.Clear
Dim iRow As Long
For iRow = LBound(Data, 1) To UBound(Data, 1)
Me.ComboBox1.AddItem Data(iRow, 1) & " " & Data(iRow, 2)
Next iRow
End Sub
Then you can select your item as follows:
And you can retrieve the value B 2
with Me.ComboBox1.Text
:
Debug.Print Me.ComboBox1.Text 'returns B 2
这篇关于使用数组中的两个文本字段填充组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!