Forum Discussion

Rufus_Bertrand's avatar
Rufus_Bertrand
Brass Contributor
Sep 28, 2021
Solved

I need to combine rows in spreadsheet that have common employee number.

I'm using Microsoft Excel for Microsoft 365 MSO version 16.0.   I have a spreadsheet of employee information, I have 3 rows per employee and need to combine those rows into 1 row adding 2 columns f...
  • HansVogelaar's avatar
    HansVogelaar
    Sep 29, 2021

    Rufus_Bertrand 

    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

Resources