简体   繁体   English

在Excel VBA中填充多列组合框

[英]Populating multi column combobox in excel vba

Hi I'm newbie in vba excel, but i didn't find yet what i'm looking for in google. 嗨,我是vba excel的新手,但是我还没有找到我在google中寻找的东西。

I want a list like this in my combo box 我想在组合框中找到这样的列表

BUS     B
APPLE   A
SUGAR   S
JELLY   J

I do like this in the past 我过去确实喜欢这样

Dim listEntries(3, 2) As Variant

listEntries(0, 0) = "A"
listEntries(0, 1) = "Apple"
listEntries(1, 0) = "S"
listEntries(1, 1) = "Sugar"
listEntries(2, 0) = "J"
listEntries(2, 1) = "Jelly"

Me.ComboBox1.List = listEntries

but now days the data become so many. 但是如今,数据变得如此之多。 I want the list is coming from another sheet list. 我希望该列表来自另一个工作表列表。 I found this in Google. 我在Google中找到了这个。 but it is still not works 但是还是不行

Private Sub UserForm_Initialize()
Dim cItem As Range
Dim ws As Worksheet

Set ws = Worksheets("LookupLists")

For Each cItem In ws.Range("ItemList")
  With Me.cboItem
    .AddItem cItem.Value
    .List(.ListCount - 1, 1) = cItem.Offset(0, 1).Value
  End With
Next cItem
End Sub

Is there any missed in my code or is there another way to get the multiple value from another sheet ? 我的代码中是否有任何遗漏的内容,或者有另一种方法可以从另一张纸上获取多次取值?

You don't need to loop if you just want to put a range in the list: 如果您只想在列表中放置一个范围,则无需循环:

Private Sub UserForm_Initialize()

Me.cboItem.List = Worksheets("LookupLists").Range("ItemList").Resize(, 2).Value

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM