Search code examples
vbacomboboxvariant

Assigning a variant to a combobox and vice-versa


I am trying to assign a variant the "value" of a combobox in a userform in order to store the list so that I can re-assign the "Value" of the variant to the combobox when initializing the userform.

Here is the code I am using to assign my variant the List value of the combobox before the userform is closed:

Dim S()
Private Sub ExitButton_Click()
ReDim S(1 To NewRecordUserForm.RepCombo.ListCount)
S = NewRecordUserForm.RepCombo.List
Unload Me
End Sub

I then use this set of code in a separate sub to initialize the combobox upon opening the userform:

Private Sub UserForm_Initialize()

NewRecordUserForm.RepCombo.List = S

End Sub

Note that Dim S() has been declared as a global variable. I get the following error message:

"Run-error '380':

Could not set the list property. Invalid property value."


Solution

  • Dim ArrRep() As Variant
    
    Public Sub PopulateCombos()
    
    NewRecordUserForm.RepCombo.List = ArrRep
    
    End Sub
    
    Public Sub SaveCombos()
    
    ReDim ArrRep(1 To NewRecordUserForm.RepCombo.ListCount)
    ArrRep = NewRecordUserForm.RepCombo.List
    
    End Sub
    

    I called the first sub upon opening the userform and the second just before it closes. Worked!