I have a spreadsheet:
Company 1 | ID1
Company 1 | ID2
Company 1 | ID3
Company 2 | ID4
Company 2 | ID5
Company 2 | ID6
Company 3 | ID7
Company 3 | ID8
Company 3 | ID9
I need to combine each company into one row, with all their IDs into columns to the right. There can be a different number of IDs per company.
The final result would look like:
Company 1 | ID1 | ID2 | ID3
Company 2 | ID4 | ID5 | ID6
Company 3 | ID7 | ID8 | ID9
How could I accomplish this? Preferably with VBA.
This single formula solution assumes that each company has the same number of rows:
Enter the following formula in D2
and ctrl-enter/copy-paste/fill-down&right into the rest of the table:
=""&INDEX($A:$B,ROW($D$2)+3*INT(ROW()-ROW($D$2))+MAX(0,COLUMN()-COLUMN($D$2)-1),2-(COLUMN()=COLUMN($D$2)))
Note that this formula will work for any (identical) number of rows for each company, and will correctly adjust if rows or columns are added/removed.
However, the formula will break if the table it is in is copied/cut and pasted elsewhere. The $D$2
s need to be changed to match the new table location's top-left data cell in order to make it work correctly again.
If there is a variable number of rows for each company, the single formula required is:
=IFERROR(IF(COLUMN()=COLUMN($D$2),""&INDEX($A:$A,MATCH(D1,$A:$A,0)+COUNTIF($A:$A,D1)),INDEX(INDEX($B:$B,MATCH($D2,$A:$A,0)):INDEX($B:$B,MATCH($D2,$A:$A,0)+COUNTIF($A:$A,$D2)-1),COLUMN()-COLUMN($D$2))),"")
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments