Combobox is not filled when user form is initialised, but filled after closing and reopening form

StackOverflow https://stackoverflow.com/questions/23643439

  •  22-07-2023
  •  | 
  •  

Question

For some odd reason, my combo boxes are not "preloaded" with content once the form is initialised, the user has to first close down the form and then open it again for the combo boxes to be prefilled. It's a bug that is starting to irritate the end user, any idea why would this happen? I've attached Boot to a button in my spreadsheet, which initialses the form. Code below:

In UserForm1

Private Sub UserForm1_Initialize()
    Call GetPrimaryContact
    Call GetSecondaryContact
End Sub

In Module1

Public itm1
Public itm2

Sub Boot()

    UserForm1.Show
    Call GetPrimaryContact
    Call GetSecondaryContact

End Sub

Sub GetPrimaryContact()

    Dim Col As New Collection
    Dim i As Long
    Dim CellVal As Variant

    ' Clear filters

    ThisWorkbook.Sheets("Master").AutoFilter.ShowAllData

    ' Get last row

    LastRow = ThisWorkbook.Sheets("Master").Cells(Rows.Count, "A").End(xlUp).Row

    ' Loop between all of column F to get unique values

    For i = 3 To LastRow
        CellVal = ThisWorkbook.Sheets("Master").Range("F" & i).Value
        On Error Resume Next
        Col.Add CellVal, Chr(34) & CellVal & Chr(34)
        On Error GoTo 0
    Next i

    ' Populate the first with primary contacts

    For Each itm1 In Col
        With UserForm1.ComboBox1
            If IsEmpty(itm1) Then .AddItem "No Contact" Else .AddItem itm1
        End With
    Next

End Sub

Sub GetSecondaryContact()

    Dim Col As New Collection
    Dim i As Long
    Dim CellVal As Variant

    ' Clear filters

    ThisWorkbook.Sheets("Master").AutoFilter.ShowAllData

    ' Get last row

    LastRow = ThisWorkbook.Sheets("Master").Cells(Rows.Count, "A").End(xlUp).Row

    ' Loop between all of column F to get unique values

    For i = 3 To LastRow
        CellVal = ThisWorkbook.Sheets("Master").Range("G" & i).Value
        On Error Resume Next
        Col.Add CellVal, Chr(34) & CellVal & Chr(34)
        On Error GoTo 0
    Next i

    ' Populate the first with primary contacts

    For Each itm2 In Col
        With UserForm1.ComboBox2
            If Not IsEmpty(itm2) Then .AddItem itm2
        End With
    Next

End Sub
Was it helpful?

Solution

You should call the functions GetPrimaryContact and GetSecondaryContact on the form initialize event, this way the control's will be loaded as intended. See the example code below.

Sub Boot()

    UserForm1.Show

End Sub

Private Sub UserForm_Initialize()

    Call GetPrimaryContact
    Call GetSecondaryContact

End Sub

OTHER TIPS

I think your problem is that your Initialize code has Userform1_Initialize. It should only be written like Userform_Initialize and it will work. And in the Sub boot put userform1.show last instead of first, If you stepping with F8 you will see that when you come to FormShow it stops there so it doesnt load your "Calls" until you close it and thats why you have them next time you start.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top