Merge Multiple rows into a single row

contemporary

New Member
Joined
Jul 27, 2015
Messages
5
Hi All,

I've searched for this, perhaps not hard enough! but I haven't see it covered the way I'm looking for it

I've a csv file like this

003R91165A4 BrandXerox
003R91165A4ColourWhite
003R91165A4Dimensions210x297mm
003R91165A4Eco-AwareYes
003R91165A4For CopiersYes
003R91165A4For Laser PrintersYes
003R91165A4Grammage80gsm
003R91165A4ManufacturerXerox
003R91165A4Recycled Product100% Recycled Material
003R91165A4SizeA4
003R91165A4TypePlain Paper
003R91166A3BrandXerox
003R91166A3ColourWhite
003R91166A3Dimensions297x420mm
003R91166A3Eco-AwareYes
003R91166A3ManufacturerXerox
003R91166A3Recycled Product100% Recycled
003R91166A3SizeA3
003R91166A3TypePlain Paper

<tbody>
</tbody>

and I am looking to get excel to combine the rows so that i can get it to show like this

003R91165A4BrandXeroxColourWhiteDimensions210x297mmEco-AwareYesFor CopiersYesFor Laser PrintersYesGrammage80gsmManufacturerXeroxRecycled Product100% Recycled MaterialSizeA4TypePlain Paper
003R91166A3BrandXeroxColourWhiteDimensions297x420mmEco-AwareYesManufacturerXeroxRecycled Product100% RecycledSizeA3TypePlain Paper

<tbody>
</tbody>

Any suggestions or tips greatly appreciated
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
contemporary,

Here is a macro solution for you to consider that uses one array in memory for the output results.

You can change the raw data worksheet name in the macro.

The macro will create a new worksheet Results for the output.

Sample raw data before the macro:


Excel 2007
ABC
110016For Use WithLaser Printer
210016Form DesignInvoice
310016Parts Per Set1-Part
410016TypeBusiness Forms
510017For Use WithInkjet/Laser Printers
610017Form DesignInvoice
710017Parts Per Set2-Part
810017TypeBusiness Forms
910018For Use WithLaser Printer
1010018Form DesignStatement
1110018Parts Per Set1-Part
1210018TypeBusiness Forms
1310021For Use WithLaser Printer
1410021Form DesignPay Advice
1510021TypeBusiness Forms
1610026CopyNCR
1710026For Use WithDot Matrix Printer
1810026Form DesignInvoice
1910026Parts Per Set2-Part
2010026TypeBusiness Forms
2110027For Use WithDot Matrix Printer
2210027Form DesignInvoice
2310027Parts Per Set3-Part
2410027TypeBusiness Forms
2510030For Use WithInkjet/Laser Printers
2610030Form DesignStatement
2710030Parts Per Set1-Part
2810030TypeBusiness Forms
2910033CopyNCR
3010033For Use WithDot Matrix Printer
3110033Form DesignPay Advice
3210033Parts Per Set3-Part
3310033TypeBusiness Forms
3410034ColourBrown
Sheet1


During, and, after the macro in the raw data worksheet:


Excel 2007
ABC
10010016For Use WithLaser Printer
20010016Form DesignInvoice
30010016Parts Per Set1-Part
40010016TypeBusiness Forms
50010017For Use WithInkjet/Laser Printers
60010017Form DesignInvoice
70010017Parts Per Set2-Part
80010017TypeBusiness Forms
90010018For Use WithLaser Printer
100010018Form DesignStatement
110010018Parts Per Set1-Part
120010018TypeBusiness Forms
130010021For Use WithLaser Printer
140010021Form DesignPay Advice
150010021TypeBusiness Forms
160010026CopyNCR
170010026For Use WithDot Matrix Printer
180010026Form DesignInvoice
190010026Parts Per Set2-Part
200010026TypeBusiness Forms
210010027For Use WithDot Matrix Printer
220010027Form DesignInvoice
230010027Parts Per Set3-Part
240010027TypeBusiness Forms
250010030For Use WithInkjet/Laser Printers
260010030Form DesignStatement
270010030Parts Per Set1-Part
280010030TypeBusiness Forms
290010033CopyNCR
300010033For Use WithDot Matrix Printer
310010033Form DesignPay Advice
320010033Parts Per Set3-Part
330010033TypeBusiness Forms
340010034ColourBrown
Sheet1


And in worksheet Results (not all columns are shown for brevity):


Excel 2007
ABCXYZAAAB
10010016For Use WithLaser Printer
20010017For Use WithInkjet/Laser Printers
30010018For Use WithLaser Printer
40010021For Use WithLaser Printer
50010026CopyNCR
60010027For Use WithDot Matrix Printer
70010030For Use WithInkjet/Laser Printers
80010033CopyNCR
90010034ColourBrown
100030110BrandSilvine
110030111BrandSilvine
120030112BrandSilvine
13003R91165A4BrandXerox
14003R91166A3BrandXerox
150050001BrandSteinbeis
160050002BrandSteinbeisSizeA4TypePlain Paper
170050004BrandSteinbeisSizeA4TypePlain Paper
180050005BrandSteinbeisSizeA4TypePlain Paper
190050006BrandSteinbeisSizeA4TypePlain Paper
200060010BrandBanner
210060015BrandEsselte
220060016BrandEsselte
Results


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
2. Open your NEW 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
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Sub ReorgDataV3()
' hiker95, 07/28/2015, ME871309
Dim w1 As Worksheet, wr As Worksheet
Dim o As Variant, j As Long, c As Long
Dim r As Long, lr As Long, rr As Long, n As Long, nmax As Long, ng As Long, t As String
Application.ScreenUpdating = False
Set w1 = Sheets("Sheet1")   '<-- you can change the sheet name here
With w1
  lr = .Cells(Rows.Count, 1).End(xlUp).Row
  For r = 1 To lr
    ng = ng + 1
    n = Application.CountIf(.Columns(1), .Cells(r, 1).Value)
    If IsNumeric(.Cells(r, 1)) Then
      t = .Cells(r, 1).Value
      With .Range(.Cells(r, 1), .Cells(r + n - 1, 1))
        .NumberFormat = "@"
        .Value = "00" & t
      End With
    End If
    If n > nmax Then nmax = n
    r = r + n - 1
  Next r
  ReDim o(1 To ng, 1 To (nmax * 2) + 1)
  For r = 1 To lr
    n = Application.CountIf(.Columns(1), .Cells(r, 1).Value)
    j = j + 1: c = 1
    o(j, 1) = .Cells(r, 1).Value
    For rr = r To r + n - 1
      c = c + 1
      o(j, c) = .Cells(rr, 2).Value
      c = c + 1
      o(j, c) = .Cells(rr, 3).Value
    Next rr
    r = r + n - 1
  Next r
End With
If Not Evaluate("ISREF(Results!A1)") Then Worksheets.Add(After:=w1).Name = "Results"
Set wr = Sheets("Results")
wr.UsedRange.Clear
With wr
  .Cells(1, 1).Resize(UBound(o, 1)).NumberFormat = "@"
  .Cells(1, 1).Resize(UBound(o, 1), UBound(o, 2)) = o
  .Columns(1).Resize(, UBound(o, 2)).AutoFit
  .Activate
End With
Application.ScreenUpdating = False
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the ReorgDataV3 macro.
 
Last edited:
Upvote 0
Thanks very much for you help, however on the result page i now found that the initial codes in column A have extra 00 so code 0010016 is now 000010016, any suggestions?
 
Upvote 0
Thanks very much for you help, however on the result page i now found that the initial codes in column A have extra 00 so code 0010016 is now 000010016, any suggestions?

contemporary,

You are vey welcome.

You can see by my screenshots that the macro does work correctly based on the last workbook you provided.

I would think that the raw data you ran the macro on did not have the same format in column A as the last workbook you provided.

I have exceeded the normal amount of time I allocate for solving problems/requests from web sites like MrExcel.

Click on the Reply to Thread button, and just put the word BUMP in the thread. Then, click on the Post Quick Reply button, and someone else will assist you.
 
Upvote 0

Forum statistics

Threads
1,214,975
Messages
6,122,538
Members
449,088
Latest member
RandomExceller01

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