vba 如何从 Excel 电子表格中的列填充组合框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14392122/
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-08 14:45:20  来源:igfitidea点击:

How do I populate a combo box from a column in my excel spread sheet?

excel-vbacomboboxspreadsheetvbaexcel

提问by GLP

I have two excel spreadsheets, one has a combobox, the other one has a list of department names. I need to populate the combobox with the department names. How do I acheive this.

我有两个 Excel 电子表格,一个有一个组合框,另一个有一个部门名称列表。我需要用部门名称填充组合框。我如何实现这一点。

回答by bonCodigo

Here is a VBA Code:

这是一个VBA代码:

Dim vArr as Variant
Dim i as Integer
vArr = WorksheetFunction.Transpose(Sheets(2).Range("A2:A10").value)
With Sheets(1).OLEObjects("ComboBox1").Object
     .Clear
     For i = Lbound(vArr) to Ubound(vArr)
        .AddItem vArr(i)
     Next i
End With

Here is the most simpler way to load the combobox, given your department range will not be empty...

鉴于您的部门范围不会为空,这是加载组合框的最简单方法...

Private Sub Workbook_Open()
    Sheets(1).ComboBox1.List = Sheets(2).Range("A2:A10").Value
End Sub

or within Sheet1:

或在 Sheet1 中:

Private Sub Worksheet_Activate()
    ComboBox1.List = Sheets(2).Range("A2:A10").Value
End Sub