[英]Excel VBA Userform Combobox - Load Multiple Columns Values
I am using this code to load values of used range in column C
. 我正在使用此代码加载列
C
中使用范围的值。 This works fine, however I need to also load values in columns E
and G
inside the combobox separated by a "-" so for example each combobox entry will read "Row 1 Col C value - Row 1 Col E value - Row 1 Col G value"
. 这可以正常工作,但是我还需要在组合框内的
E
和G
列中加载值,并用“-”分隔,因此例如每个组合框条目将显示为"Row 1 Col C value - Row 1 Col E value - Row 1 Col G value"
。
Private Sub UserForm_Initialize()
Dim lastrow As Long
Dim ws As Worksheet
ws = mysheet
lastrow = ws.Columns("C").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
tag_combo.List = ws.Range("C" & ws.Range("start_row_pu").Row + 1 & ":" & "C" & lastrow).Value2
End If
End Sub
Example call joining column values 调用联接列值示例
As close as possible to your original post you can read all values into a 1-based 2-dim temporary array, join 1st, 3rd and 5th column values by &
and assign them back to the comboboxe's .List
property: 尽可能接近原始帖子,您可以将所有值读入基于1的2维临时数组,并通过
&
将第1,第3和第5列的值连接起来&
并将它们分配回组合框的.List
属性:
Private Sub UserForm_Initialize()
Dim ws As Worksheet
Set ws = Worksheets("Tabelle1")
Dim lastrow As Long, i As Long
lastrow = ws.Columns("A").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Dim v As Variant
v = ws.Range("C" & ws.Range("start_row_pu").Row + 1 & ":" & "G" & lastrow).Value2
For i = LBound(v) To UBound(v) ' join column values C, E, G
v(i, 1) = v(i, 1) & " - " & v(i, 3) & " - " & v(i, 5)
Next i
ReDim Preserve v(1 to Ubound(v), 1 to 1) ' redimension temporary array to 1 column
ComboBox1.List = v ' assign array back to .List property
End Sub
Further notes due to question in comment 由于评论有疑问,需要进一步注意
After having assigned range data (eg C2:G4711
) to a 2-dimensioned 1-based variant array v
you have to loop through the 1-based array data now, where 在将范围数据(例如
C2:G4711
)分配给基于二维的基于1的变量数组v
您现在必须遍历基于1的数组数据,其中
LBound(v)
always starts from "row" 1
in a 1-based array (so callede lower boundary) and LBound(v)
始终从基于1的数组中的“行” 1
开始(所谓的下边界),并且 UBound(v)
returns the upper boundary, eg 4710
(=4711 - 2 + 1 as starting from the second row) "rows" ; UBound(v)
返回上限, 例如4710
(= 4711-2-2从第二行开始) “ rows” ; now you refer to column C
data in the original range via "column" index 1
of the variant array, ie v(i, 1)
, to E
via index 3
: v(i, 3)
, to G
via index 5
: v(i, 5)
. 现在你指的柱
C
经由“列”的索引数据中的原始范围1
所述变体阵列的,即, v(i, 1)
以E
经由索引3
: v(i, 3)
以G
经由索引5
: v(i, 5)
。 The above example joins the 1st, the 3rd and the 5th value in the array column items via the &
connector and assigns the resulting string back to the first array column thus (over)writing the already read in data of range column C
. 上面的例子中通过加入第一,第三和在阵列列项的第五值
&
连接器和所述生成的字符串从而(过)分配回到第一阵列列写入范围列的数据已经读C
。
Eventually you have to redimension the original 5 array columns to only one in order to represent your wanted data row connected now by " - "
. 最终,您必须将原始的5个数组列重新分配为仅1个,以便表示现在通过
" - "
连接的所需数据行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.