使用VBA在excel中使用相同ID进行多个数据搜索

时间:2018-11-25 15:46:40

标签: excel vba excel-vba

此照片突出显示Worksheet是具有相同标识的数据,但其中一个列中的数据不同。在下面的代码中,我能够搜索相同标识的数据之一,但不会以我的形式显示。 UserForm。我需要的是单击搜索按钮时,以相同的RF号显示2个不同的数据。

VBA代码

Private Sub CommandButton1_Click()
Dim x As Long
Dim y As Long

x = Sheets("ONGOING").Range("A" & Rows.Count).End(xlUp).Row 
For y = 1 To x
If Sheets("ONGOING").Cells(y, 1).Text = TextBox1.Value Then
TextBox1.Text = Sheets("ONGOING").Cells(y, 1)
TextBox2.Text = Sheets("ONGOING").Cells(y, 3 )
TextBox3.Text = Sheets("ONGOING").Cells(y, 5)
TextBox4.Text = Sheets("ONGOING").Cells(y, 8)
TextBox5.Text = Sheets("ONGOING").Cells(y, 9)
TextBox6.Text = Sheets("ONGOING").Cells(y, 6)
TextBox7.Text = Sheets("ONGOING").Cells(y, 7)
ComboBox1.Text = Sheets("ONGOING").Cells(y, 4)
ComboBox2.Text = Sheets("ONGOING").Cells(y, 2)
End If
Next y

End Sub

2 个答案:

答案 0 :(得分:0)

也许您需要这个:

Private Sub CommandButton1_Click()
    Dim x As Long
    Dim y As Long
    Dim found As Boolean ' add a boolean variable

    With Sheets("ONGOING")
        x = .Range("A" & .Rows.Count).End(xlUp).Row
        For y = 1 To x
            If .Cells(y, 1).Text = TextBox1.Value Then
                If Not found Then ' if first matching RF
                    found = True ' mark current RF as already found at least once
                    TextBox1.Text = .Cells(y, 1)
                    TextBox2.Text = .Cells(y, 3)
                    TextBox3.Text = .Cells(y, 5)
                    TextBox4.Text = .Cells(y, 8)
                    TextBox5.Text = .Cells(y, 9)
                    TextBox6.Text = .Cells(y, 6)
                    TextBox7.Text = .Cells(y, 7)
                    ComboBox1.Text = .Cells(y, 4)
                    ComboBox2.Text = .Cells(y, 2)
                Else 'otherwise
                    TextBox3.Text = TextBox3.Text & "," & .Cells(y, 5) 'update items list textbox
                End If
            End If
        Next y
    End With

End Sub

答案 1 :(得分:0)

这里是没有for循环的快速解决方案。实际上,它会在每次点击时四舍五入-例如如果您有3个项目具有相同的RF#,则每次单击时将显示1、2、3、1、2、3 ...,依此类推。

Private Sub CommandButton1_Click()

'we set the cell from where we start the search as static - 
'it keeps its value even after the macro has ended.
Static varFoundCell As Variant 
Dim rSource As Range 'declaring ranges just to make it clearer
Dim rTarget As Range

Set rSource = ThisWorkbook.Worksheets("Ongoing").Range("A1").CurrentRegion

'if cell is empty it means the previous search did not give a result - so we start looking from the first cell again.
If IsEmpty(varFoundCell) Then Set varFoundCell = rSource.Cells(1, 1) 

'we looking for RF# in the first column of the source range and return the cell where it is found
Set rTarget = rSource.Columns(1).Find(TextBox1.Text, varFoundCell, LookIn:=xlValues)

'if we found it then we assigne the cell it is in as the cell we start our next search from
If Not (rTarget Is Nothing) Then Set varFoundCell = rTarget

'we found the cell, we get its row to take outher data from the source range
TextBox2.Text = rSource(rTarget.Row, 2)
'the rest of controls go here below

End Sub