Closed Thread
Results 1 to 11 of 11

Thread: Macro for transposing multiple columns to rows

  1. #1
    Registered User
    Join Date
    02-16-2011
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    4

    Macro for transposing multiple columns to rows

    Hello.

    I've read a lot of the threads in regards to transposing columns to rows but I have been unable to find anything that works as well as I need it too.

    I have 69 columns with data in each cell, for 65 rows. I need to transpose column A into a row, and then take columns B to BQ and add them to the end of that row. So not a new row, but the same one. I understand that this is going to be a very long row, but it is the only way my statistics program will accept the data.

    I have to repeat this macro for up to 40 participants data so if anyone helps me out with a macro, could you please add comments so that I can understand which bit does what? Especially since I may not have to use all 69 colums, but just perhaps 59.

    Any help would be greatly appreciated. The task ahead of me is depressing!

    Thank you in advance!

  2. #2
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: Macro for transposing multiple columns to rows

    Hi nique... Welcome to the forum.

    Click GO ADVANCED and use the paperclip icon to post up a desensitized copy of your workbook.

    Make sure it has BEFORE and AFTER examples so we can see what you want.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    02-16-2011
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Macro for transposing multiple columns to rows

    I've attached an example of the excel document.

    There are 6 columns as an example - and you can see that at the bottom of the columns I have tranposed them all into one row.

    Like I said before, I need that done - but for 69 columns of data.
    Attached Files Attached Files

  4. #4
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: Macro for transposing multiple columns to rows

    Try this:
    Option Explicit
    
    Sub TransposeColumnDatasToRow()
    Dim LR As Long, Rw As Long, LC As Long, Col As Long
    Dim MyArr As Variant
    
    Application.ScreenUpdating = False
    LR = Range("A" & Rows.Count).End(xlUp).Row
    Rw = LR + 3
    LC = Cells(1, Columns.Count).End(xlToLeft).Column
    
        For Col = 1 To LC
            MyArr = Application.WorksheetFunction _
                .Transpose(Range(Cells(1, Col), Cells(LR, Col)))
            Cells(Rw, (LR * Col) - (LR - 1)).Resize(, UBound(MyArr)).Value = MyArr
        Next Col
    
    Application.ScreenUpdating = True
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  5. #5
    Registered User
    Join Date
    02-16-2011
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Macro for transposing multiple columns to rows

    JB. You are amazing!

    I spent the entire day yesterday just recording a macro and repeating the same steps for each column. My code is about 300 lines long. Horrible.

    Can I ask for one more favour? Could you possibly explain to me what each line of code does, or what the key components to it are? This will not only aid in my understanding but also help me edit it whenever I need to tweek the code a little bit.

    Again, thank you ever so much.

  6. #6
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: Macro for transposing multiple columns to rows

    Option Explicit         'turn on code checking, helps eliminate mistakes
    
    Sub TransposeColumnDatasToRow()
    Dim LR As Long          'used to store last row of data
    Dim Rw As Long          'stores target row for transposed data
    Dim LC As Long          'stores last column with data in it
    Dim Col As Long         'the current column being transposed
    Dim MyArr As Variant    'array to store column data in tranposed format
    
    Application.ScreenUpdating = False                  'speed up macro, no screen flicker
    LR = Range("A" & Rows.Count).End(xlUp).Row          'find last row of data
    Rw = LR + 3                                         'set the target row
    LC = Cells(1, Columns.Count).End(xlToLeft).Column   'find last column with data
    
    
        For Col = 1 To LC                               'loop columns from A to last column
          'tranpose col into array
            MyArr = Application.WorksheetFunction _
                .Transpose(Range(Cells(1, Col), Cells(LR, Col)))
          'paste array values into target row, the destination column
          'each time is found by multiplying last row by the current column num
          'and subtracting all but one from the last row count.
          'for instance if LR is 68 and we are in col 1 right now, then the math
          'is:  (68*1)-(68-1)=(68)-(67)=1, so the target column would be 1. Next col
          'would be (68*2)-(68-1)=(136)-(67)=69, so second target column is 69. etc.
          'Once target column is found on target row, we resize the target cells by
          'the number of values currently in the array.  Then we drop in the array.
            Cells(Rw, (LR * Col) - (LR - 1)).Resize(, UBound(MyArr)).Value = MyArr
        Next Col
    
    Application.ScreenUpdating = True       'turn screenupdating back to normal, view results
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  7. #7
    Registered User
    Join Date
    02-16-2011
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Macro for transposing multiple columns to rows

    Seriously no idea what I would have done without you. Thank you ever so much.

  8. #8
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: Macro for transposing multiple columns to rows

    If that takes care of your need, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  9. #9
    Registered User
    Join Date
    09-04-2011
    Location
    Illinois, United States
    MS-Off Ver
    Excel 2011
    Posts
    2

    Re: Macro for transposing multiple columns to rows

    I have a list of companies and their information in column A which goes down to row 3600. I'm trying to use a macro to transpose the company information into individual rows rather than one big row like what the code in the macro for the topic has.

    I attached a small example file. I need the rows to transpose like the first row in the file that is in bold.

    Any help would be appreciated. Thanks!
    Attached Files Attached Files

  10. #10
    Registered User
    Join Date
    09-04-2011
    Location
    Illinois, United States
    MS-Off Ver
    Excel 2011
    Posts
    2

    Re: Macro for transposing multiple columns to rows

    Hi, I have a list of companies and their information in column A which goes down to row 3600. I'm trying to use a macro to transpose the company information into individual rows rather than one big row like what the code in the macro for the topic has.

    I attached a small example file(maybe in my post below). I need the rows to transpose like the first row in the file that is in bold.

    Any help would be appreciated. Thanks!

  11. #11
    Extremely Helpful member
    Join Date
    12-23-2006
    Location
    Belgium
    MS-Off Ver
    XL2003
    Posts
    6,127

    Re: Macro for transposing multiple columns to rows

    Your post does not comply with Rule 2 of our Forum RULES. Don't post a question in the thread of another member -- start your own thread. If you feel it's particularly relevant, provide a link to the other thread.
    Quoting entire posts clutters the forum and makes threads hard to read !

    If you are pleased with a member's answer then use the Star icon to rate it

    Click here to see forum rules

Closed Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0