Monday, July 29, 2013

Transpose duplicate data from columns to rows in excel.

I was working for a project when I came across a difficulty there were more than 14000 rows but there were getting repeated with reference to column A. I had to write a macro to move duplicate columns to rows. Now i am sharing it with you people. dont forget to click ad and leave reviews




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 

No comments:

Post a Comment