簡體   English   中英

Excel VBA +基於帶或不帶循環的ComboBox中的值填充多個文本框

[英]Excel VBA + Fill multiple Text boxes based on values in ComboBox with or without loop

主席先生,我正在努力解決這個問題。 請幫忙。 我在用戶表單上制作了許多文本框,例如textbox1、2、3、4、5、6、7、8至16。我有這樣的數據,例如,我想在Combobox中放置一個名稱,並在其中顯示First搜索結果的Name。 TextBox1,textbox2中的Age,Textbox3中的City,然后在下一個單元格中循環搜索相同的值,如果找到,則在Textbox 4中找到他的名字,Textbox5中的Age,Textbox 6中的City,依此類推(最多找到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

請幫我解決這個問題。 我已經搜索了數百個網站,但此刻仍然停留。 非常感謝

您不會感染下一條記錄。 這是您的循環的快速修復:

    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

您可以使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM