繁体   English   中英

在 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM