将选定的 ComboBox 索引传递给数组 VBA
Passing selected ComboBox indices to an array VBA
我有一个问题,希望你能回答。
我正在尝试编写一个用户窗体,其中包含 19 个组合框,所有组合框都具有相同的值(员工姓名)以用于轮班时间表。每个 ComboBox 中有 5 个条目(5 名员工)。到目前为止,一切都很好。
我现在正在尝试的是,在单击用户窗体中的按钮后将选定的索引(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
我有一个问题,希望你能回答。 我正在尝试编写一个用户窗体,其中包含 19 个组合框,所有组合框都具有相同的值(员工姓名)以用于轮班时间表。每个 ComboBox 中有 5 个条目(5 名员工)。到目前为止,一切都很好。 我现在正在尝试的是,在单击用户窗体中的按钮后将选定的索引(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