You are here:

# Excel/merge rows based on cell

Question
I had asked the question below and you had offered to write a macro for me.  I had tried to respond but I guess it didn't post.  If you could write the macro for me, I would appreciate it.  The data is sorted.  Let me know if you need any other information.  Thanks!!!

Question:
If I have data like this (5 columns):

35   1   2   3   4
35   5   6   7   8
35   9  10  11  12
40  13  14  15  16
40  17  18  19  20
42  21  22  23  24
42  25  26  27  28
42  29  30  31  32

and I needed this result below by comparing the firt column, what formula would I use?

35   1   2   3   4   5   6   7   8   9  10  11  12
40  13  14  15  16  17  18  19  20
42  21  22  23  24  25  26  27  28  29  30  31  32

Thanks!

With the variable length of data, I'm not able to think of a normal function that would do this - my SUGGESTION would be to use a macro to get the data transformed - if that is an acceptable solution, I'm happy to write such a macro - is the original data already sorted, or would it need to check all rows for a match?

This macro should do the job - it will write the data to sheet2 (so there needs to be a blank sheet2, or the reference needs changing) and it assumes that the data starts in A1 on the active sheet - amend as necessary.  If you need further help, do ask - and if it helps my direct email is aidan.heritage@virgin.net

Sub sorter()
Dim n, m
n = 1
m = 2
Dim outvar As Long
outvar = 1
Dim holdvar
holdvar = Range("A1").Value
While Cells(n, 1).Value <> ""
'need to store the data onto an output sheet
Sheets("Sheet2").Cells(outvar, 1).Value = holdvar
Sheets("Sheet2").Cells(outvar, m).Value = Cells(n, 2).Value
Sheets("Sheet2").Cells(outvar, m + 1).Value = Cells(n, 3).Value
Sheets("Sheet2").Cells(outvar, m + 2).Value = Cells(n, 4).Value
Sheets("Sheet2").Cells(outvar, m + 3).Value = Cells(n, 5).Value
n = n + 1
If Cells(n, 1).Value <> holdvar Then
'different so reset M and outvar goes up one
outvar = outvar + 1
m = 2
holdvar = Cells(n, 1).Value
Else
'increase M by 4
m = m + 4
End If
Wend

End Sub

Questioner's Rating
 Rating(1-10) Knowledgeability = 10 Clarity of Response = 10 Politeness = 10 Comment Thanks so much for your help.

This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

#### Aidan Heritage

##### Expertise

I have provided first hand support since `95 for Microsoft Office majoring in Word and Excel - support for all versions from 2 onwards. I'm based in the UK, so please allow for time differences when asking me questions from other parts of the world!

##### Experience

My background is in the insurance industry and call centre areas, but have been called upon to provide many varied solutions.

Education/Credentials
I'm educated to UK A level standard, but as I left school some 30 years ago that is rather irrelevent - university of life has provided more of a background!