Forms 初始化用户表单时,组合框不填充,但在关闭和重新打开表单后填充

Forms 初始化用户表单时,组合框不填充,但在关闭和重新打开表单后填充,forms,vba,excel,combobox,Forms,Vba,Excel,Combobox,出于某些奇怪的原因,我的组合框没有“预加载”内容。一旦表单初始化,用户必须先关闭表单,然后再次打开表单,以便预填充组合框。这是一个开始激怒最终用户的bug,你知道为什么会发生这种情况吗?我在电子表格中的一个按钮上附加了Boot,用于初始化表单。代码如下: 在用户表单1中 Private Sub UserForm1_Initialize() Call GetPrimaryContact Call GetSecondaryContact End Sub Public itm1 Pu

出于某些奇怪的原因,我的组合框没有“预加载”内容。一旦表单初始化,用户必须先关闭表单,然后再次打开表单,以便预填充组合框。这是一个开始激怒最终用户的bug,你知道为什么会发生这种情况吗?我在电子表格中的一个按钮上附加了Boot,用于初始化表单。代码如下:

在用户表单1中

Private Sub UserForm1_Initialize()
    Call GetPrimaryContact
    Call GetSecondaryContact
End Sub
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
在模块1中

Private Sub UserForm1_Initialize()
    Call GetPrimaryContact
    Call GetSecondaryContact
End Sub
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

您应该在表单初始化事件上调用函数GetPrimaryContact和GetSecondaryContact,这样可以按预期加载控件。请参见下面的示例代码

Sub Boot()

    UserForm1.Show

End Sub

Private Sub UserForm_Initialize()

    Call GetPrimaryContact
    Call GetSecondaryContact

End Sub

我认为您的问题在于您的初始化代码有Userform1\u Initialize。它只应该像Userform_Initialize那样编写,并且可以工作。
在子引导中,将userform1.show放在最后而不是第一位,如果按F8键,您将看到当您来到FormShow时,它会停止在那里,因此在您关闭它之前它不会加载您的“调用”,这就是为什么您下次启动时会有这些调用。

对不起,没有看到这段代码。正如Percy所说,应该在初始化函数上编写useform而不是userform1。