I am using find function to match ID in Report Worksheet with the ID in Data worksheet and return the data to the ID in Report Worksheet if there is a match. For unique with multiple match, the code is only returning the ID data which every is last match in the search range in Master worksheet. The code ignores the all the possible match till the last match and returns the data of last match.
Code behaving, In report worksheet, ID 313165 will only show pineapple. It will ignore Apple and kiwi.
I need help with:
1: How do I make the ID search till the end of ID Column in Data Worksheet? If the ID has multiple match then return the multiple corresponding data in single-cell with a new line in report worksheet. 2: How can we add text joint or something which will paste multiple value in same cell.
The table will help in guiding the question.
[TABLE="class: grid, width: 500, align: left"]
<tbody>[TR]
[TD]Data
wkst[/TD]
[TD][/TD]
[TD][/TD]
[TD]Report
wkst[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Col A[/TD]
[TD]Col B[/TD]
[TD][/TD]
[TD]Col A[/TD]
[TD]Col B[/TD]
[/TR]
[TR]
[TD]ID[/TD]
[TD]Data[/TD]
[TD][/TD]
[TD]ID[/TD]
[TD]Data[/TD]
[/TR]
[TR]
[TD]313165[/TD]
[TD]Apple[/TD]
[TD][/TD]
[TD]313165[/TD]
[TD]Apple
kiwi
pineapple[/TD]
[/TR]
[TR]
[TD]164207[/TD]
[TD]Green Apple[/TD]
[TD][/TD]
[TD]164208[/TD]
[TD]Orange[/TD]
[/TR]
[TR]
[TD]164208[/TD]
[TD]Orange[/TD]
[TD][/TD]
[TD]312313[/TD]
[TD]Mango
Carrot[/TD]
[/TR]
[TR]
[TD]313165[/TD]
[TD]kiwi[/TD]
[TD][/TD]
[TD]312357[/TD]
[TD]Banana[/TD]
[/TR]
[TR]
[TD]312313 [/TD]
[TD]Mango[/TD]
[TD][/TD]
[TD]164566[/TD]
[TD]Mandarin[/TD]
[/TR]
[TR]
[TD]312357[/TD]
[TD]Banana[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]312313[/TD]
[TD]Carrot[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]164566[/TD]
[TD]Mandarin[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]313165[/TD]
[TD]Pineapple[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">Sub Match_Data()
Dim wsM As Worksheet 'Master worksheet from where the data is copied
Dim wsR As Worksheet 'Report Worksheet where the data will be copied, The id to look for are store in this worksheet
Dim firstMatchRow As Long
Dim i As Long ' To start Counter
Dim LastRow As Long 'To check for last used row in ID columns in report worksheet
Dim rngMatch As Range ' To define range where the match has to be found,in master worksheet
LastRow = wsR.Range("A" & wsR.Rows.Count).End(xlUp).Row 'Check for the last row in column A in ID worksheet.
Set wsM = Worksheets("DATA") 'Worksheet where the data is coming from,it is a source worksheet
Set wsR = Worksheets("ID") 'Worksheet where the information will be paste if the condition is satisfied
For i = 2 To lngLastRow 'counter from i=2 to last used row
Set rngMatch = wsM.Range("A:A").Find( _
What:=wsR.Range("A" & i).Value, _
LookAt:=xlPart) 'Range (A:A) is where the data will be looked in Data worksheet, Find is what we are looking for from the ID and jump to next row with i counter,
'xlPart is what it will be looking at, instead of xlWhole I have used xlpart.
If Not rngMatch Is Nothing Then
firstMatchRow = rngMatch.Row
Do
wsR.Range("B" & i).Value = rngMatch.Offset(0, 1).Value
Set rngMatch = wsM.Range("A:A").FindNext(rngMatch)
Loop Until firstMatchRow = rngMatch.Row
Else
wsR.Range("C" & i).Value = "NOT FOUND"
End If
Next i
End Sub
</code>
Code behaving, In report worksheet, ID 313165 will only show pineapple. It will ignore Apple and kiwi.
I need help with:
1: How do I make the ID search till the end of ID Column in Data Worksheet? If the ID has multiple match then return the multiple corresponding data in single-cell with a new line in report worksheet. 2: How can we add text joint or something which will paste multiple value in same cell.
The table will help in guiding the question.
[TABLE="class: grid, width: 500, align: left"]
<tbody>[TR]
[TD]Data
wkst[/TD]
[TD][/TD]
[TD][/TD]
[TD]Report
wkst[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Col A[/TD]
[TD]Col B[/TD]
[TD][/TD]
[TD]Col A[/TD]
[TD]Col B[/TD]
[/TR]
[TR]
[TD]ID[/TD]
[TD]Data[/TD]
[TD][/TD]
[TD]ID[/TD]
[TD]Data[/TD]
[/TR]
[TR]
[TD]313165[/TD]
[TD]Apple[/TD]
[TD][/TD]
[TD]313165[/TD]
[TD]Apple
kiwi
pineapple[/TD]
[/TR]
[TR]
[TD]164207[/TD]
[TD]Green Apple[/TD]
[TD][/TD]
[TD]164208[/TD]
[TD]Orange[/TD]
[/TR]
[TR]
[TD]164208[/TD]
[TD]Orange[/TD]
[TD][/TD]
[TD]312313[/TD]
[TD]Mango
Carrot[/TD]
[/TR]
[TR]
[TD]313165[/TD]
[TD]kiwi[/TD]
[TD][/TD]
[TD]312357[/TD]
[TD]Banana[/TD]
[/TR]
[TR]
[TD]312313 [/TD]
[TD]Mango[/TD]
[TD][/TD]
[TD]164566[/TD]
[TD]Mandarin[/TD]
[/TR]
[TR]
[TD]312357[/TD]
[TD]Banana[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]312313[/TD]
[TD]Carrot[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]164566[/TD]
[TD]Mandarin[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]313165[/TD]
[TD]Pineapple[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">Sub Match_Data()
Dim wsM As Worksheet 'Master worksheet from where the data is copied
Dim wsR As Worksheet 'Report Worksheet where the data will be copied, The id to look for are store in this worksheet
Dim firstMatchRow As Long
Dim i As Long ' To start Counter
Dim LastRow As Long 'To check for last used row in ID columns in report worksheet
Dim rngMatch As Range ' To define range where the match has to be found,in master worksheet
LastRow = wsR.Range("A" & wsR.Rows.Count).End(xlUp).Row 'Check for the last row in column A in ID worksheet.
Set wsM = Worksheets("DATA") 'Worksheet where the data is coming from,it is a source worksheet
Set wsR = Worksheets("ID") 'Worksheet where the information will be paste if the condition is satisfied
For i = 2 To lngLastRow 'counter from i=2 to last used row
Set rngMatch = wsM.Range("A:A").Find( _
What:=wsR.Range("A" & i).Value, _
LookAt:=xlPart) 'Range (A:A) is where the data will be looked in Data worksheet, Find is what we are looking for from the ID and jump to next row with i counter,
'xlPart is what it will be looking at, instead of xlWhole I have used xlpart.
If Not rngMatch Is Nothing Then
firstMatchRow = rngMatch.Row
Do
wsR.Range("B" & i).Value = rngMatch.Offset(0, 1).Value
Set rngMatch = wsM.Range("A:A").FindNext(rngMatch)
Loop Until firstMatchRow = rngMatch.Row
Else
wsR.Range("C" & i).Value = "NOT FOUND"
End If
Next i
End Sub
</code>