Concatenate Multiple Rows

spersad

New Member
Joined
Feb 22, 2011
Messages
15
Hi all:

I have a spreadsheet with 5500 rows. I am looking to join certain rows by a unique identifier. My column layout is as follows:

Account Name Current Investments
foo1 PE
foo1 FoHF
foo1 HF
foo2 HF
foo3 RE
foo3 PE

What I would like for my data upload is:

Account Name Current Investments
foo1 PE, FoHF, HF
foo3 RE, PE

Where the unique identifer = Account Name. Is there a way to do this with the CONCATENATE and IF functions in excel ?


Thanks in advance for the help.


Lenee
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
spersad,

If you are looking for a VBA Macro solution:


Sample raw data before the macro:


Excel Workbook
AB
1Account NameCurrent Investments
2foo1PE
3foo1FoHF
4foo1HF
5foo2HF
6foo3RE
7foo3PE
8
Sheet1





After the macro:


Excel Workbook
AB
1Account NameCurrent Investments
2foo1PE, FoHF , HF
3foo2HF
4foo3RE, PE
5
6
7
8
Sheet1





Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).


1. Copy the below code, by highlighting the code and pressing the keys CTRL + C
2. Open your workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.


Code:
Option Explicit
Sub ReorgData()
' hiker95, 04/17/2011
' http://www.mrexcel.com/forum/showthread.php?t=544187
' Thank you VoG, http://www.mrexcel.com/forum/showthread.php?t=540814
Dim Area As Range, LR As Long, a As Long
Dim At As String, Bt As String
Application.ScreenUpdating = False
At = Range("A1").Value
Bt = Range("B1").Value
Rows(1).Delete
LR = Range("A" & Rows.Count).End(xlUp).Row
For a = LR To 2 Step -1
  If Range("A" & a).Value <> Range("A" & a - 1).Value Then Rows(a).Insert
Next a
Columns("A").Copy Destination:=Range("E1")
Columns("A").Delete
For Each Area In Columns("A").SpecialCells(xlCellTypeConstants).Areas
  If Area.Rows.Count > 1 Then
    Area(1).Offset(, 1).Value = Join(Application.Transpose(Area), ", ")
  Else
    Area(1).Offset(, 1).Value = Area(1).Value
  End If
Next Area
Columns("D").Copy Destination:=Range("A1")
Columns("D").Delete
Columns("B").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Rows(1).Insert
Range("A1").Value = At
Range("B1").Value = Bt
Columns("A:B").AutoFit
Application.ScreenUpdating = True
End Sub


Then run the ReorgData macro.
 
Upvote 0
spersad,

Your data must be grouped/sorted before you run the macro.


The above macro will replace the original data with the results.

If you would like to keep the original data, and have the results in different columns, or in a new worksheet, then let me know.
 
Last edited:
Upvote 0
Hi Hiker95:

Thanks for the help. I tried it, and it seem to work well. I did leave out one important fact. :)

I have 22 columns, address info, contact numbers, etc. They are linked to account name. Is there a way to extend the macro to align the entire row when you do the join ? Would it be easier to send you the spreadsheet to explain it better?


Thanks,

Leneee
 
Upvote 0
spersad,

You will generally get much more help (and faster) in this forum if you can post your small samples (what you have and what you expect to achieve) directly in the forum.

To attach screenshots, see below in my Signature block: Post a screen shot with one of these:

If you are not able to give us screenshots, see below in my Signature block: You can upload your workbook to Box Net
 
Upvote 0
spersad,


For display purposes I have only shown columns A thru C (of columns A thru V) (sensative information changed).


Sample raw data before the updated macro:


Excel Workbook
ABC
1Account_NameCurrent_InvestmentsC
21794 ComHedge FundC2
31875 FinLong-Only FundC3
41st GloFund of Hedge Funds, Fund of Private Equity FundsC4
52 PM PortLong-Only FundC5
621NorthHedge Fund, Private Equity FundC6
73 BankFund of Hedge FundsC7
83 BankHedge FundC8
93 BankLong-Only FundC9
103 DegreeHedge FundC10
11
12
13
All (2)





After the updated macro:


Excel Workbook
ABC
1Account_NameCurrent_InvestmentsC
21794 ComHedge FundC2
31875 FinLong-Only FundC3
41st GloFund of Hedge FundsC4
51st GloFund of Private Equity FundsC4
62 PM PortLong-Only FundC5
721NorthHedge FundC6
821NorthPrivate Equity FundC6
93 BankFund of Hedge FundsC7
103 BankHedge FundC8
113 BankLong-Only FundC9
123 DegreeHedge FundC10
13
All (2)





Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).


1. Copy the below code, by highlighting the code and pressing the keys CTRL + C
2. Open your workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.


Code:
Option Explicit
Sub ReorgDataV2()
' hiker95, 04/19/2011
' http://www.mrexcel.com/forum/showthread.php?t=544187
Dim LR As Long, a As Long
Dim Sp, s As Long
Application.ScreenUpdating = False
LR = Cells(Rows.Count, 2).End(xlUp).Row
For a = LR To 2 Step -1
  If InStr(Cells(a, 2), ",") > 0 Then
    Sp = Split(Cells(a, 2), ", ")
    s = UBound(Sp)
    Range("A" & a + 1 & ":V" & a + 1).Resize(s).Rows.Insert
    Range("A" & a & ":V" & a).Copy Range("A" & a + 1 & ":V" & a + 1).Resize(s)
    Cells(a, 2).Resize(s + 1).Value = Application.Transpose(Sp)
  End If
Next a
Application.ScreenUpdating = True
End Sub



Before you run the macro, save you workbook, Save As, a macro enabled workbook.


Then run the ReorgDataV2 macro.
 
Upvote 0
Hi Hiker95:

Thank you for this. I tried the macro and its not joining the rows. Am I doing something wrong? I copied into a test workbook and ran the macro as written.

Before macro:

A B C
1 Account_Name Current_Investments C
2 1794 Com Hedge Fund C2
3 1875 Fin Long-Only Fund C3
4 1st Glo Fund of Hedge Funds C4
5 1st Glo Fund of Private Equity Funds C4
6 2 PM Port Long-Only Fund C5
7 21North Hedge Fund C6
8 21North Private Equity Fund C6
9 3 Bank Fund of Hedge Funds C7
10 3 Bank Hedge Fund C8
11 3 Bank Long-Only Fund C9
12 3 Degree Hedge Fund C10
13

After macro:

A B C
1 Account_Name Current_Investments C
2 1794 Com Hedge Fund C2
3 1875 Fin Long-Only Fund C3
4 1st Glo Fund of Hedge Funds C4
5 1st Glo Fund of Private Equity Funds C4
6 2 PM Port Long-Only Fund C5
7 21North Hedge Fund C6
8 21North Private Equity Fund C6
9 3 Bank Fund of Hedge Funds C7
10 3 Bank Hedge Fund C8
11 3 Bank Long-Only Fund C9
12 3 Degree Hedge Fund C10
13
 
Upvote 0
spersad,


If the worksheet name containing the raw data is always All (2), then let me know and I will adjust the macro.


With your raw data already sorted/grouped together per your Box.Net workbook.


Sample raw data in the first worksheet (left most worksheet in your workbook):


Excel Workbook
ABC
1Account_NameCurrent_InvestmentsType
21794 Commodore FundsHedge FundC2
31875 Finance SALong-Only FundC3
41st GlobalFund of Hedge Funds, Fund of Private Equity FundsC4
52 PM Portfolio ManagementLong-Only FundC5
621North AdvisorsHedge Fund, Private Equity FundC6
73 Banken Generali Investment-GesellschaftFund of Hedge FundsC7
83 Banken Generali Investment-GesellschaftHedge FundC8
93 Banken Generali Investment-GesellschaftLong-Only FundC9
103 Degrees Asset Management Pte. Ltd.Hedge FundC10
113A Alternative Asset Advisors SA (Syz & Co)Hedge FundC11
123A Asia LimitedHedge FundC12
1347 Degrees North Capital ManagementHedge FundC13
144finance, Signer Burtscher & CoHedge FundC14
154finance, Signer Burtscher & CoLong-Only FundC15
166800 Capital, LLCHedge FundC16
17747 CapitalPrivate Equity FundC17
18A&G Banca PrivadaHedge FundC18
19A&O Services (Netherland)Fund of Private Equity FundsC19
20A.H. Zeppa Family FoundationHedge FundC20
21A.M.&C. Finance SAFund of Hedge FundsC21
22AAL Advisors At Large SAFund of Hedge FundsC22
23Aargauische PensionskasseFund of Hedge FundsC23
24AB Electrolux Pension FundPrivate Equity FundC24
25AB Electrolux Pension FundReal Estate FundC25
26Abacus Planning Group, Inc.Real Estate FundC26
27ABB, Inc.Fund of Private Equity FundsC26
28ABB, Inc.Hedge FundC28
29ABB, Inc.Private Equity FundC29
30ABB, Inc.Real Estate FundC30
31***
All (2)





After the macro in a new worksheet Results:


Excel Workbook
ABC
1Account_NameCurrent_InvestmentsType
21794 Commodore FundsHedge FundC2
31875 Finance SALong-Only FundC3
41st GlobalFund of Hedge Funds, Fund of Private Equity FundsC4
52 PM Portfolio ManagementLong-Only FundC5
621North AdvisorsHedge Fund, Private Equity FundC6
73 Banken Generali Investment-GesellschaftFund of Hedge Funds, Hedge Fund, Long-Only FundC7
83 Degrees Asset Management Pte. Ltd.Hedge FundC10
93A Alternative Asset Advisors SA (Syz & Co)Hedge FundC11
103A Asia LimitedHedge FundC12
1147 Degrees North Capital ManagementHedge FundC13
124finance, Signer Burtscher & CoHedge Fund, Long-Only FundC14
136800 Capital, LLCHedge FundC16
14747 CapitalPrivate Equity FundC17
15A&G Banca PrivadaHedge FundC18
16A&O Services (Netherland)Fund of Private Equity FundsC19
17A.H. Zeppa Family FoundationHedge FundC20
18A.M.&C. Finance SAFund of Hedge FundsC21
19AAL Advisors At Large SAFund of Hedge FundsC22
20Aargauische PensionskasseFund of Hedge FundsC23
21AB Electrolux Pension FundPrivate Equity Fund, Real Estate FundC24
22Abacus Planning Group, Inc.Real Estate FundC26
23ABB, Inc.Fund of Private Equity Funds, Hedge Fund, Private Equity Fund, Real Estate FundC26
24***
Results








Code:
Option Explicit
Sub ReorgDataV3()
' hiker95, 04/19/2011
' http://www.mrexcel.com/forum/showthread.php?t=544187
Dim w1 As Worksheet, wR As Worksheet
Dim LR As Long, a As Long, aa As Long, SR As Long, ER As Long, H As String
Application.ScreenUpdating = False
Set w1 = Worksheets(1)
If Not Evaluate("ISREF(Results!A1)") Then Worksheets.Add(After:=w1).Name = "Results"
Set wR = Worksheets("Results")
wR.UsedRange.Clear
w1.Columns(1).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=wR.Columns(1), Unique:=True
wR.Range("B1:V1").Value = w1.Range("B1:V1").Value
LR = wR.Cells(Rows.Count, 1).End(xlUp).Row
For a = 2 To LR Step 1
  SR = Application.Match(wR.Cells(a, 1), w1.Columns(1), 0)
  ER = Application.Match(wR.Cells(a, 1), w1.Columns(1), 1)
  H = ""
  w1.Rows(SR).Copy wR.Rows(a)
  For aa = SR To ER Step 1
    H = H & w1.Cells(aa, 2) & ", "
  Next aa
  If Right(H, 2) = ", " Then H = Left(H, Len(H) - 2)
  wR.Cells(a, 2) = H
Next a
wR.Columns("A:B").AutoFit
wR.Activate
Application.ScreenUpdating = True
End Sub



Before you run the macro, save your workbook, Save As, a macro enabled workbook.


Then run the ReorgDataV3 macro.
 
Upvote 0

Forum statistics

Threads
1,224,564
Messages
6,179,543
Members
452,924
Latest member
JackiG

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