Search code examples
arraysexcelvbacomboboxuserform

Passing selected ComboBox indices to an array VBA


I have a question, which I hope you can answer. I'm trying to programm a UserForm which contains 19 ComboBoxes all with the same value (names of employees) for a shift schedule. There are 5 entries in each ComboBox (5 employees). So far so good. What I'm trying now is, to pass the selected indices (0, 1, 2, 3, 4) to an array upon clicking a button in the userform. At the end I should have an array with 19 entries (for each combobox). My problem now is, that I don't know if the indices were passed succesfully to my array "Larray". Below is the code

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


Solution

  • Perhaps you want this?

    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