【问题标题】:Excel VBA + Fill multiple Text boxes based on values in ComboBox with or without loopExcel VBA +根据ComboBox中的值填充多个文本框,带或不带循环
【发布时间】:2017-06-21 17:23:27
【问题描述】:

先生,我正在努力解决这个问题。请帮忙。我在用户表单上制作了许多文本框,例如 textbox1、2、3、4、5、6、7、8 到 16。我有这样的数据 我想在组合框中输入一个名字,并希望在 TextBox1 中显示第一个搜索结果的名称,在 Textbox3 中显示年龄在 textbox2 中的城市,然后在下一个单元格中循环搜索相同的值,如果找到,则在文本框 4 中显示他的名字,在 Textbox5 中显示年龄,在文本框 6 等(最多可找到 4 个结果)。我正在使用以下代码,但它仅在 Textbox1、Textbox2 和 Textbox3 中显示第一个找到的结果,但在 Textbox4、5 和 6 中没有弹出第二个找到的结果。请帮助:代码如下

Private Sub CommandButton1_Click()
    Dim fnd As String, FirstFound As String
    Dim FoundCell As Range, rng As Range
    Dim myRange As Range, LastCell As Range
    Dim a, b, c As Integer

    a = 1
    b = 2
    c = 3

    fnd = ComboBox1.Value

    Set myRange = Sheets("Data").Range("B4:B50")
    Set LastCell = myRange.Cells(myRange.Cells.Count)
    Set FoundCell = myRange.Find(What:=fnd, After:=LastCell)

    If Not FoundCell Is Nothing Then
        FirstFound = FoundCell.Address
    Else
        GoTo NothingFound
    End If

    Set rng = FoundCell

    Do Until FoundCell Is Nothing
        UserForm1.Controls("TextBox" & a).Text = FoundCell.Offset(0, 0).Value
        UserForm1.Controls("TextBox" & b).Text = FoundCell.Offset(0, 1).Value
        UserForm1.Controls("TextBox" & c).Text = FoundCell.Offset(0, 2).Value                     
        a = a + 3
        b = b + 3
        c = c + 3

        If FoundCell.Address = FirstFound Then Exit Do
    Loop

    Exit Sub

NothingFound:
    TextBox1.Text = "not found"
    TextBox2.Text = "not found"
    TextBox3.Text = "not found"
End Sub

请帮我解决这个问题。我已经搜索了数百个网站,但停留在这一点上。非常感谢提前

【问题讨论】:

  • 感谢您的解决方案

标签: excel vba


【解决方案1】:

你可以使用AutoFilter(),假设单元格“B3”有一个标题:

Option Explicit

Private Sub CommandButton1_Click()
    Dim fnd As String
    Dim txtBoxOffset As Long, j As Integer
    Dim cell As Range

    If Me.ComboBox1.ListIndex = -1 Then Exit Sub

    fnd = Me.ComboBox1.Value
    With Sheets("Data").Range("B3:B50")
        .AutoFilter field:=1, Criteria1:=fnd
        If Application.WorksheetFunction.Subtotal(103, .Cells) > 1 Then
            For Each cell In .Resize(.Rows.Count - 1).Offset(1).SpecialCells(xlCellTypeVisible)
                For j = 1 To 3
                    Me.Controls("TextBox" & (txtBoxOffset + j)).Text = cell.Offset(, j - 1)
                Next
                txtBoxOffset = txtBoxOffset + 3
            Next
        Else
            For j = 1 To 3
                Me.Controls("TextBox" & j).Text = "not found"
            Next
        End If
        .Parent.AutoFilterMode = False
    End With
End Sub

【讨论】:

  • @MohdAsif,你通过了吗?
  • 感谢您的解决方案
  • 不客气。如果您关心“解决方案长度”问题,那么请考虑上面的 all 您需要的代码,即它替代了您在 OP 中的 整个 代码
【解决方案2】:

您没有复制下一条记录。这是您的循环的快速修复:

    Do
        UserForm1.Controls("TextBox" & a).Text = FoundCell.Offset(0, 0).value
        UserForm1.Controls("TextBox" & b).Text = FoundCell.Offset(0, 1).value
        UserForm1.Controls("TextBox" & c).Text = FoundCell.Offset(0, 2).value
        a = a + 3
        b = b + 3
        c = c + 3
        Set FoundCell = myRange.Find(What:=fnd, After:=FoundCell) ' <-- get next record
    Loop Until FoundCell.Address = FirstFound '<-- Stop when retrieveing again the same first record

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-09
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    相关资源
    最近更新 更多