Showing posts with label move duplicate data to rows. Show all posts
Showing posts with label move duplicate data to rows. Show all posts

Transpose duplicate data from columns to rows in excel.

I was working on a project where I encountered a challenge. There were over 14,000 rows of data, and many rows were repeated based on values in Column A. To address this, I wrote a macro to move duplicate entries into rows, aligning them horizontally. I’m sharing this macro with you—feel free to use it and leave feedback!



VBA Macro: Rearrange Duplicate Columns into Rows

Sub rearrange()
Dim a, n As Long, i As Long, u()
Dim g, m As Integer, k As Integer
a = Range("A1").CurrentRegion
n = UBound(a, 1)
ReDim u(1 To n, 1 To 5)
For i = 1 To n
    If a(i, 1) <> g Then
        k = k + 1
        g = a(i, 1)
        m = 2
        u(k, 1) = a(i, 1)
        u(k, 2) = a(i, 2)
    Else
        m = m + 1
        If m > 5 Then ReDim Preserve u(1 To n, 1 To m)
        u(k, m) = a(i, 2)
    End If
Next i
u(1, 1) = "Macro output"
Range("E1").Resize(k, UBound(u, 2)) = u
End Sub 

How to Use:

  1. Copy the code into the VBA editor (press Alt + F11 in Excel, then go to Insert > Module).
  2. Run the macro on a dataset where Column A has duplicate identifiers and Column B has corresponding values.
  3. The output will appear starting from Column E, with duplicates arranged in rows.