множественный поиск данных с одинаковым идентификатором в Excel с использованием VBA - PullRequest
0 голосов
/ 25 ноября 2018

Выделена эта фотография Рабочий лист - это данные с той же идентификацией, но с разными данными в одном столбце.В приведенном ниже коде я смог найти одну из данных с той же идентификацией, но она не будет отображаться в моей форме. UserForm .Мне нужно было показать 2 разных данных с одним и тем же номером RF, когда я нажимаю кнопку поиска.

Код 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 голосов
/ 25 ноября 2018

Вот быстрое решение без цикла.Фактически он будет вращаться при каждом клике - например, если у вас есть 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
0 голосов
/ 25 ноября 2018

может быть, вам нужно это:

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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...