如何在 Excel VBA 用户窗体中创建两列查找组合框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13371892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 18:29:57  来源:igfitidea点击:

How to Create Two Columns Lookup Combobox in Excel VBA UserForm

vbaexcel-vbaexcel

提问by user1760110

Can you please let me know how I can create a Two columns Look up combobox in Excel VBA UserForm? I am looking to create some thing like this:

你能告诉我如何在 Excel VBA 用户窗体中创建一个两列查找组合框吗?我正在寻找创建这样的东西:

enter image description here

在此处输入图片说明

I know we can add Items to combobox using a method like this:

我知道我们可以使用这样的方法将项目添加到组合框:

Private Sub UserForm_Initialize()
  With Me.ComboBox1
    .AddItem "215"
    .AddItem "316"
    .AddItem "485"
   End With
End Sub

but I need to generate a associated value with 215,316,485 and so on valyes like hammer,... Thanks for your time,

但我需要生成一个与 215,316,485 等值相关的值,例如锤子,...谢谢您的时间,

回答by barrowc

Fill a two-dimensional array and set the Listproperty of the ComboBox to that array:

填充一个二维数组并将ListComboBox的属性设置为该数组:

Dim listEntries(3, 2) As Variant

listEntries(0, 0) = "215"
listEntries(0, 1) = "Hammer"
listEntries(1, 0) = "316"
listEntries(1, 1) = "Wrench"
listEntries(2, 0) = "485"
listEntries(2, 1) = "Pliers"

Me.ComboBox1.List = listEntries

You may also need to adjust the ColumnWidthsand TextColumnproperties accordingly

您可能还需要相应地调整ColumnWidthsTextColumn属性