VBA - USERFORM - 查找equall值并为每个匹配项在行到文本框中输入相邻值

时间:2017-02-07 07:56:01

标签: excel vba excel-vba

我需要将此代码升级到find.next版本。附件是样本表格,以便更好地理解。 Keycombobox值可以多找一次,相邻值的每个匹配都必须在相邻的文本框中。

DATA SEMPLE Keytextbox值= TEST1

.Cells(row with FIRST find TEST1, 1) = textbox10 (located in multipage.page(find))
.Cells(row with SECOND find TEST1, 1) = textbox110 (locateted in multipage.page(alternative find))

enter image description here

Option Explicit

    Sub TestFind()

    Dim sonsat As Long
    Dim FindRng As Range

    With Sheets("DATA")
        Set FindRng = .Range("A:A").Find(Keycombobox.Text) ' <-- assuming  Keycombobox is a textBox

        If Not FindRng Is Nothing Then ' <-- successful find
            sonsat = FindRng.Row

            ' rest of yout code here ....
            .Cells(sonsat, 1) = TextBox10 '<-- for good coding practice use TextBox1.Value ' or TextBox1.Text
            .Cells(sonsat, 2) = TextBox20
            .Cells(sonsat, 3) = TextBox30
            .Cells(sonsat, 4) = TextBox40
            .Cells(sonsat, 5) = TextBox50
            .Cells(sonsat, 6) = TextBox60
            .Cells(sonsat, 7) = TextBox70
        Else
            MsgBox "Unable to find " & Keycombobox.Text & " in specified Range !"
        End If
    End With

    End Sub

1 个答案:

答案 0 :(得分:1)

可能是你之后:

Sub TestFind()
    Dim f As Range
    Dim firstAddress As String
    Dim iPage As Long, i As Long

    With Sheets("DATA").Range("A:A").SpecialCells(xlCellTypeConstants)
        Set f = .Find(what:=Keycombobox.Text, LookIn:=xlvalkue, lookat:=xlWhole) ' <-- assuming  Keycombobox is a textBox
        If Not f Is Nothing Then
            firstAddress = f.address
            Do
                For i = 1 To 7
                    Me.Controls("TextBox" & iPage + i * 10) = .Cells(f.Row, i)
                Next
                iPage = iPage + 100
                Set f = .FindNext(f)
            Loop While f.address <> firstAddress
        End If
    End With
End Sub