Forum Discussion
I need to combine rows in spreadsheet that have common employee number.
- Sep 29, 2021
Here is a new macro:
Sub CombineRows() Dim r As Long Dim c As Long Dim m As Long Application.ScreenUpdating = False ' Get the last row number m = Cells(Rows.Count, 5).End(xlUp).Row ' Loop through the rows in groups of 3 For r = 2 To m Step 3 ' Loop through columns 12 (L) to 14 (N) For c = 12 To 14 ' Set the first of the three cells to the sum of the three Cells(r, c).Value = Application.Sum(Cells(r, c).Resize(3)) Next c Next r ' Remove the duplicates Range("A1:R" & m).RemoveDuplicates Columns:=5, Header:=xlYes Application.ScreenUpdating = True End Sub
Hi again Hans!
Thanks for the macro!
I copied it into the test data I sent you and I'm uploading the results.
It looks like it's not getting the correct values from columns L-N?
I copied macro as delivered and didn't change anything.
Also, I searched for the 'Step 3' command in the 'For r = 2 To Step 3' line in Google and didn't find any information on it. Can you explain what this is doing?
thanks
Rufus
thanks!
- HansVogelaarSep 29, 2021MVP
You wrote "The columns I want to include on 1st row are columns 'M' and 'N'"
Did you perhaps intend "The columns I want to include on 1st row are columns 'L, M' and 'N'"?
- Rufus_BertrandSep 29, 2021Brass Contributor
Hi Hans,
I'm uploading file showing the desired output. Sorry I should have started with that.
thanks
Rufus- HansVogelaarSep 29, 2021MVP
Here is a new macro:
Sub CombineRows() Dim r As Long Dim c As Long Dim m As Long Application.ScreenUpdating = False ' Get the last row number m = Cells(Rows.Count, 5).End(xlUp).Row ' Loop through the rows in groups of 3 For r = 2 To m Step 3 ' Loop through columns 12 (L) to 14 (N) For c = 12 To 14 ' Set the first of the three cells to the sum of the three Cells(r, c).Value = Application.Sum(Cells(r, c).Resize(3)) Next c Next r ' Remove the duplicates Range("A1:R" & m).RemoveDuplicates Columns:=5, Header:=xlYes Application.ScreenUpdating = True End Sub
- Rufus_BertrandSep 29, 2021Brass Contributorthanks Hans, this code works great!
Thanks for saving the day again:)
Rufus
- HansVogelaarSep 29, 2021MVP
I see that I interpreted your original question differently. I'll take a look.
- Rufus_BertrandSep 29, 2021Brass ContributorSorry for my foggy explanation:(
My query that created the file returns one row for each month for each employee. I would like to combine the 3 months into 1 row. Hope that sheds more light on what i'm requesting.
thanks for all your help!