如何用 Excel 行(字符串)填充组合框?

How to fill combobox with an Excel-row (string)?

我想在 Excel

中用一行(猫、狗、鱼、...)填充组合框

到目前为止我所做的是以下内容

Private Sub UserForm_Initialize()

    Dim Axis As Variant

    Axis = Rows(1)

    ComboBox1.List = Axis

End Sub

编译有效,但我只能看到第一个值(例如 CAT)。

如果我尝试以下代码....

Private Sub UserForm_Initialize()

    Dim Axis As Variant

    Axis = Columns(1)              '<< Columns instead of Rows

    ComboBox1.List = Axis

End Sub

...组合框包含整个列。

我尝试了很多方法,但还没有找到解决方案。

所以我想问问你们是否有人可以帮助我。

谢谢

如果您有一行并希望它们显示在一个列表中,您必须转置

ComboBox1.List = Application.Transpose(Sheet1.Range("A1:C1").Value)

你也可以

ComboBox1.List = Array("Cat", "Dog", "Fish")

要使一行保持一行,您必须增加组合框的列数。

我更喜欢使用以下代码,这样我就可以轻松地删除或添加引用中列出的项目 table。

Dim wS As Worksheet
Dim refTable As Range

Set wS = Sheet1    
Set refTable = wS.Range("A1" , Cells(Rows.Count, "A").End(xlUp))
Combobox.List = refTable.Value

您可以更改 refTable 范围以更改 "A1" 和 "A"。 它也可以用于更改行 Rows.Count 和 xlUp.

对我有用的是:

Dim axis as Variant
Dim lstColumn As Long

'Find last Column
    With ActiveSheet.UsedRange
        lstColumn = .Columns(.Columns.Count).Column
    End With

'Fill axis with all values from the first row
axis = Application.ActiveSheet.Range(Cells(1, 1), Cells(1, lstColumn))

'Write all values of axis in Combobox
ComboBox11.Column = axis