且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Excel VBA - 将值插入多列组合框

更新时间:2023-11-30 18:56:40

对于多列组合框,您必须使用 .List 属性来填充列数据.所以在你的情况下:

For multi column ComboBoxes you must use the .List property to fill the columns data. So in your case:

For Each aCell In copyFrom

 If aCell.Value <> "" And aCell.Column = Range("RegEvents_EventID").Column Then
  ComboBox2.AddItem aCell.Value
  lRow = aCell.Row
 End If

 If aCell.Row = lRow And aCell.Column = Range("RegEvents_Event").Column Then
  With ComboBox2
   .List(.ListCount - 1, 1) = aCell.Value
  End With
 End If

Next

使用 .TextColumn 在文本字段中显示两列的完整示例.要求:带有 ComboBox1 的用户表单

A complete Example using .TextColumn displaying both columns in the text field. Requirements: UserForm with ComboBox1

Private Sub UserForm_Initialize()

 With Me.ComboBox1
  .ColumnCount = 3
  .BoundColumn = 2
  .TextColumn = 3
  .ColumnWidths = "1cm;1cm;0"
 End With

 RegEvents_EventID = [{1;2;3;4;5}]
 RegEvents_Event = [{"Event 1";"Event 2";"Event 3";"Event 4";"Event 5"}]

 For i = LBound(RegEvents_EventID) To UBound(RegEvents_EventID)
  With Me.ComboBox1
   .AddItem RegEvents_EventID(i, 1)
   .List(.ListCount - 1, 1) = RegEvents_Event(i, 1)
   .List(.ListCount - 1, 2) = RegEvents_EventID(i, 1) & " " & RegEvents_Event(i, 1)
  End With
 Next

End Sub