Arrays 将选定的组合框索引传递给数组VBA

Arrays 将选定的组合框索引传递给数组VBA,arrays,excel,vba,combobox,userform,Arrays,Excel,Vba,Combobox,Userform,我有一个问题,希望你能回答。 我正在尝试编写一个UserForm,其中包含19个组合框,所有组合框的值(员工姓名)都相同,用于轮班计划。每个组合框中有5个条目(5名员工)。到现在为止,一直都还不错。 我现在尝试的是,单击userform中的按钮后将所选索引(0、1、2、3、4)传递给数组。最后,我应该有一个包含19个条目的数组(对于每个组合框)。 我现在的问题是,我不知道索引是否成功地传递给了我的数组“Larray”。下面是代码 Public Sub cmd_Erstellen_Click()

我有一个问题,希望你能回答。 我正在尝试编写一个UserForm,其中包含19个组合框,所有组合框的值(员工姓名)都相同,用于轮班计划。每个组合框中有5个条目(5名员工)。到现在为止,一直都还不错。 我现在尝试的是,单击userform中的按钮后将所选索引(0、1、2、3、4)传递给数组。最后,我应该有一个包含19个条目的数组(对于每个组合框)。 我现在的问题是,我不知道索引是否成功地传递给了我的数组“Larray”。下面是代码

Public Sub cmd_Erstellen_Click()

Dim j As Long
Dim Larray As Variant

For j = 1 To 19

Me.Controls("ComboBox" & j).ListIndex = Larray

Next j



Unload UserForm1


End Sub

也许你想要这个

Public Sub cmd_Erstellen_Click()

Dim j As Long
Dim Larray(1 To 19) As Variant

For j = 1 To 19
    Larray(j) = Me.Controls("ComboBox" & j).ListIndex
    Debug.Print j & ", " & Larray(j) 'show result in immediate window
Next j

Unload UserForm1

End Sub

请这样做:

Public Sub cmd_Erstellen_Click()
 Dim j As Long, Larray(18) As Variant

 For j = 1 To 19
    Larray(j - 1) = Me.Controls("ComboBox" & j).ListIndex
 Next j
 Debug.Print Join(Larry, ",") 'it returns the array as string in Immediate Window
                              'only to see if it is what you expect...

 Unload UserForm1
End Sub
(1) 赋值需要另一种方式(2),如果您颠倒该行,代码将为同一变量赋值19个值。