Find multiple instances

BennyH

New Member
Joined
Apr 27, 2015
Messages
11
Hi,

this is driving me nuts. I want lookup 1 record-Op Number from a sheet and combine multiple instances onto the next sheet. Tried using Vlookup but only returns the first instance. Example:
Op Num

<tbody>
</tbody><colgroup><col></colgroup>
ID
2TT3

<tbody>
</tbody><colgroup><col></colgroup>
33804
2TT3

<tbody>
</tbody><colgroup><col></colgroup>
33805

<tbody>
</tbody>






Op NumID1ID2
2TT33380433805

<tbody>
</tbody>

Any help would be appreciated.

<tbody>
</tbody><colgroup><col><col></colgroup>
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Hi,

Take a look at this if this helps you:


Excel 2016 (Windows) 64 bit
ABCDE
1Op NumID
22TT333804
32TT333805
42TT412345
52TT423456
62TT333806
72TT333807
8
9
10
11
12Op NumID1ID2ID3ID4
132TT333804338053380633807
Sheet1
Cell Formulas
RangeFormula
B13{=INDEX($B$2:$B$7,SMALL(IF($A$13=$A$2:$A$7,ROW($A$2:$A$7)-ROW($A$2)+1),RIGHT(B12,1)))}
C13{=INDEX($B$2:$B$7,SMALL(IF($A$13=$A$2:$A$7,ROW($A$2:$A$7)-ROW($A$2)+1),RIGHT(C12,1)))}
D13{=INDEX($B$2:$B$7,SMALL(IF($A$13=$A$2:$A$7,ROW($A$2:$A$7)-ROW($A$2)+1),RIGHT(D12,1)))}
E13{=INDEX($B$2:$B$7,SMALL(IF($A$13=$A$2:$A$7,ROW($A$2:$A$7)-ROW($A$2)+1),RIGHT(E12,1)))}
Press CTRL+SHIFT+ENTER to enter array formulas.
 
Last edited:
Upvote 0
Hi,

This should work if you need a VBA answer just replace the sheet numbers (Sheet1 = your data sheet ,Sheet2 = Output sheet) with your sheet names. I assuming your OP No's are in column "A".

Public Sub instances()
Dim inst_1 As Integer, lrow As Integer, insert_row As Integer, val As Integer, x As Integer, lrow1 As Integer
lrow = Sheets("sheet1").Range("a1").End(xlDown).Row
If Sheets("sheet2").Cells(1, 1).Value = "" Then
lrow1 = Sheets("sheet2").Cells(1, 1).Row - 1
Else
lrow1 = Sheets("sheet2").Cells(1, 1).End(xlDown).Row
End If
input1 = InputBox("Op Number")
For x = 1 To lrow
If Sheets("sheet1").Cells(x, 1).Value = input1 Then
op = input1
End If
Next x

If op = "" Then
MsgBox "No Op Number Found!"
Exit Sub
Else
rows_no = WorksheetFunction.CountIf(Sheets("Sheet1").Range("a2:a" & lrow), op)

Dim array1()
ReDim array1(rows_no - 1)

insert_row = 0

For i = 0 To lrow
If Sheets("Sheet1").Cells(i + 1, 1).Value = op Then
array1(insert_row) = Sheets("sheet1").Cells(i + 1, 2).Value
insert_row = insert_row + 1
End If
Next

insert_row = 0

Sheets("sheet2").Cells(lrow1 + 1, 1).Value = op

For i = 1 To rows_no
Sheets("sheet2").Cells(lrow1 + 1, i + 1).Value = array1(insert_row)
insert_row = insert_row + 1
Next
End If
End Sub
 
Upvote 0
There is a bug in the last script use this if you need to do this with multiple Op No.'s

Public Sub instances()
Dim inst_1 As Integer, lrow As Integer, insert_row As Integer, val As Integer, x As Integer, lrow1 As Integer
lrow = Sheets("sheet1").Range("a1").End(xlDown).Row
If Sheets("sheet2").Cells(1, 1).Value = "" Then
lrow1 = Sheets("sheet2").Cells(1, 1).Row - 1
ElseIf Sheets("sheet2").Cells(2, 1).Value = "" Then
lrow1 = Sheets("sheet2").Cells(1, 1).End(xlUp).Row
Else
lrow1 = Sheets("sheet2").Cells(1, 1).End(xlDown).Row
End If
input1 = InputBox("Op Number")
For x = 1 To lrow
If Sheets("sheet1").Cells(x, 1).Value = input1 Then
op = input1
End If
Next x

If op = "" Then
MsgBox "No Op Number Found!"
Exit Sub
Else
rows_no = WorksheetFunction.CountIf(Sheets("Sheet1").Range("a2:a" & lrow), op)

Dim array1()
ReDim array1(rows_no - 1)

insert_row = 0

For i = 0 To lrow
If Sheets("Sheet1").Cells(i + 1, 1).Value = op Then
array1(insert_row) = Sheets("sheet1").Cells(i + 1, 2).Value
insert_row = insert_row + 1
End If
Next

insert_row = 0

Sheets("sheet2").Cells(lrow1 + 1, 1).Value = op

For i = 1 To rows_no
Sheets("sheet2").Cells(lrow1 + 1, i + 1).Value = array1(insert_row)
insert_row = insert_row + 1
Next
End If
End Sub
 
Upvote 0
Hi,

Take a look at this if this helps you:

Excel 2016 (Windows) 64 bit
ABCDE
1Op NumID
22TT333804
32TT333805
42TT412345
52TT423456
62TT333806
72TT333807
8
9
10
11
12Op NumID1ID2ID3ID4
132TT333804338053380633807

<colgroup><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet1

Array Formulas
CellFormula
B13{=INDEX($B$2:$B$7,SMALL(IF($A$13=$A$2:$A$7,ROW($A$2:$A$7)-ROW($A$2)+1),RIGHT(B12,1)))}
C13{=INDEX($B$2:$B$7,SMALL(IF($A$13=$A$2:$A$7,ROW($A$2:$A$7)-ROW($A$2)+1),RIGHT(C12,1)))}
D13{=INDEX($B$2:$B$7,SMALL(IF($A$13=$A$2:$A$7,ROW($A$2:$A$7)-ROW($A$2)+1),RIGHT(D12,1)))}
E13{=INDEX($B$2:$B$7,SMALL(IF($A$13=$A$2:$A$7,ROW($A$2:$A$7)-ROW($A$2)+1),RIGHT(E12,1)))}

<thead>
</thead><tbody>
</tbody>
Entered with Ctrl+Shift+Enter. If entered correctly, Excel will surround with curly braces {}.
Note: Do not try and enter the {} manually yourself

<tbody>
</tbody>
Thanks but it didn't quite work-appreciate the reply. I found an old example from Chandoo, that works using a helper column.
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,728
Members
448,987
Latest member
marion_davis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top