在 VBA 的交互式用户窗体中添加 ComboBox

Add ComboBox in an Interactive UserForm in VBA

我正在使用本教程 (https://www.excel-easy.com/vba/examples/interactive-userform.html) 了解如何创建一个交互式用户窗体,该窗体基于一个简单的条件覆盖值,如果 ID 存在,则更新或编辑行。

但是,这对于 TextBox 非常有效,但我很难从工具箱中添加其他控件。目前,我正在尝试在循环中添加 ComboBox,以便它可以从 ComboBox 添加值。

Private Sub ComboBox1_Change()

End Sub

Private Sub CommandButton1_Click()
EditAdd
End Sub

Private Sub CommandButton2_Click()
ClearForm
End Sub

Private Sub CommandButton3_Click()
Unload Me
End Sub

Private Sub TextBox1_Change()
GetData
End Sub

Private Sub UserForm_Click()

End Sub

Private Sub UserForm_Initialize()
TextBox1.SetFocus
    ComboBox1.AddItem "One"
    ComboBox1.AddItem "Two"
    ComboBox1.AddItem "Three"
    ComboBox1.AddItem "Four"
    ComboBox1.AddItem "Five"
End Sub

这是模块。我试图通过在 for 循环中添加 UserForm.Controls("ComboBox" & j).Value = Cells(i + 1, j).Value 来修改它,但我只得到错误。

Dim id As Integer, i As Integer, j As Integer, flag As Boolean

Sub GetData()

If IsNumeric(UserForm.TextBox1.Value) Then
    flag = False
    i = 0
    id = UserForm.TextBox1.Value

    Do While Cells(i + 1, 1).Value <> ""

        If Cells(i + 1, 1).Value = id Then
            flag = True
            For j = 2 To 6
                UserForm.Controls("TextBox" & j).Value = Cells(i + 1, j).Value
                UserForm.Controls("ComboBox" & j).Value = Cells(i + 1, j).Value
            Next j
        End If


        i = i + 1

    Loop

    If flag = False Then
        For j = 2 To 6
            UserForm.Controls("TextBox" & j).Value = ""
            UserForm.Controls("ComboBox" & j).Value = ""
        Next j
    End If

Else
    ClearForm
End If

End Sub

Sub ClearForm()

For j = 1 To 6
    UserForm.Controls("TextBox" & j).Value = ""
    UserForm.Controls("ComboBox" & j).Value = ""
Next j

End Sub

Sub EditAdd()

Dim emptyRow As Long

If UserForm.TextBox1.Value <> "" Then
    flag = False
    i = 0
    id = UserForm.TextBox1.Value
    emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1

    Do While Cells(i + 1, 1).Value <> ""

        If Cells(i + 1, 1).Value = id Then
            flag = True
            For j = 2 To 6
                Cells(i + 1, j).Value = UserForm.Controls("TextBox" & j).Value
                Cells(i + 1, j).Value = UserForm.Controls("ComboBox" & j).Value
            Next j
        End If

        i = i + 1

    Loop

    If flag = False Then
        For j = 1 To 6
            Cells(emptyRow, j).Value = UserForm.Controls("TextBox" & j).Value
            Cells(emptyRow, j).Value = UserForm.Controls("ComboBox" & j).Value
        Next j
    End If

End If

End Sub

如何将 ComboBox 添加到我的模块中,以便用户窗体在 ID 存在时覆盖现有值?

我无法提供完整的答案,因为我对您要实现的目标了解不够 - 因此代码中有各种评论和查询。也许它足以让您弄清楚您必须做什么。

Dim id As Integer, i As Integer, j As Integer, flag As Boolean

Sub GetData()

If IsNumeric(UserForm.TextBox1.Value) Then 'separate check required for combobox?
    flag = False
    i = 0
    id = UserForm.TextBox1.Value

    Do While Cells(i + 1, 1).Value <> ""
        If UserForm.ComboBox1.Value = Cells(i + 1, j).Value Then 'not sure if this check is right and the j needs to be replaced with something
            'do something
            'should this be a separate flag?
        End If
        If Cells(i + 1, 1).Value = id Then
            flag = True
            For j = 2 To 6
                UserForm.Controls("TextBox" & j).Value = Cells(i + 1, j).Value
            Next j
        End If
        i = i + 1
    Loop

    If flag = False Then 'might need changing if separate flags required
        UserForm.ComboBox1.Value = ""
        For j = 2 To 6
            UserForm.Controls("TextBox" & j).Value = ""
        Next j
    End If

Else
    ClearForm
End If

End Sub