+ Reply to Thread
Results 1 to 5 of 5

How to transpose non-uniform data in multiple columns into multiple rows?

Hybrid View

  1. #1
    Registered User
    Join Date
    09-16-2011
    Location
    Hong Kong
    MS-Off Ver
    Excel 2003
    Posts
    52

    Question How to transpose non-uniform data in multiple columns into multiple rows?

    Hi

    I have a large amount of non-uniform data that I need to alter from multiple columns to multiple row.
    See example in Part A to Part B (Sometimes the data can spread over 20 columns for the same row). Is there any Macro that can transpose the data in the form of Part A to the form of Part B? Thanks.

    Part A
    Column 1,Column 2,Column 3,Column 4,Column 5,Column 6, Column 7
    Team A,Jimmy,Peter,May,Mary,Ivy,Alex
    Team B,Ryan,Ken,Julia
    Team C,John,Noel,May,Kristyn
    Team D,Mark

    Part B
    Column 1,Column 2,
    Team A,Jimmy,
    Team A,Peter
    Team A,May
    Team A,Mary
    Team A,Ivy
    Team A,Alex
    Team B,Ryan
    Team B,Ken
    Team B,Julia
    Team C,John
    Team C,Noel
    Team C,May
    Team C,Kristyn
    Team D,Mark
    Last edited by alexxgalaxy; 01-03-2013 at 08:40 PM.

  2. #2
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: How to transpose non-uniform data in multiple columns into multiple rows?

    Maybe:

    Sub alexxgalaxy()
    
    Dim lr As Long
    Dim rcell As Range
    Dim i As Long
    
    Application.ScreenUpdating = False
    
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    
    For i = 1 To lr
    
        Range(Range("A" & i), Range("G" & i)).Copy
        
        Range("I" & Rows.Count).End(3)(2).PasteSpecial Transpose:=True
        
    Next i
    
    lr = Cells(Rows.Count, 9).End(xlUp).Row
    
    For Each rcell In Range("I2:I" & lr)
    
        If Left(rcell, 4) = "Team" Then
        
            rcell.Cut rcell.Offset(, -1)
            
        End If
        
    Next rcell
    
    Range("I2").Delete xlUp
    
    For Each rcell In Range("H2:H" & lr)
    
    If rcell.Offset(, 1) <> "" Then
    
        rcell.Offset(1, 1).Select
        
            Do Until ActiveCell = ""
            
                ActiveCell.Offset(, -1).Value = rcell.Value
                
                ActiveCell.Offset(1).Select
                
            Loop
            
            End If
    
    Next rcell
    
    Range("H1:I" & lr).SpecialCells(xlCellTypeBlanks).Delete xlUp
    
    Columns("A:G").Delete xlToLeft
    
    Application.ScreenUpdating = True
    
    End Sub

  3. #3
    Registered User
    Join Date
    09-16-2011
    Location
    Hong Kong
    MS-Off Ver
    Excel 2003
    Posts
    52

    Re: How to transpose non-uniform data in multiple columns into multiple rows?

    Hi John

    This works well for the example I gave but it didn't satisfy the situation that I mentioned "Sometimes the data can spread over 20 columns for the same row". Thanks for so much for the reply, though!!

  4. #4
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: How to transpose non-uniform data in multiple columns into multiple rows?

    Another way

    Option Explicit
    Sub abcd()
     Dim a, i As Long, ii As Long, n As Long
     
     ReDim a(1 To Rows.Count, 1 To 2)
     
     With ActiveSheet
        For i = 1 To .Cells(Rows.Count, "a").End(xlUp).Row
        
           For ii = 2 To .Cells(i, Columns.Count).End(xlToLeft).Column
               n = n + 1
               a(n, 1) = .Cells(i, 1)
               a(n, 2) = .Cells(i, ii)
           Next
        Next
        .UsedRange.ClearContents
        .Cells(1).Resize(n, 2) = a
     End With
    End Sub
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  5. #5
    Registered User
    Join Date
    09-16-2011
    Location
    Hong Kong
    MS-Off Ver
    Excel 2003
    Posts
    52

    Re: How to transpose non-uniform data in multiple columns into multiple rows?

    Hi Mike

    This works perfect! And the syntax was a lot shorter than I expected. Also simpler, I might be able to understand how it works.

    Thank you so much!!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1