Consulting

Results 1 to 12 of 12

Thread: Transposing each set of 22 items (all from the same column) into rows

  1. #1
    VBAX Newbie
    Joined
    Nov 2016
    Posts
    5
    Location

    Transposing each set of 22 items (all from the same column) into rows

    Hello. I need help transposing a huge amount of data in excel through the use of vba. All of it is in one column (M) and I need to transpose every 22 values into its own row. The entire column has 7282 values or goes down to M7282. These rows need to start in column O. So M1-M22 would be transposed and would start in O1, M23-M44 would be transposed and would start in O2, etc. Help would be greatly appreciated as I have multiple spreadsheets that I would need to use this macro on.



    Thanks,
    Kiba

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Option Explicit
    
    Sub Transposer22()
    Const MCol As Long = 13
    Dim Ocel As Range
    Dim i As Long
    Dim LastRow As Long
    
    With ActiveSheet
    LastRow = .Cells(Rows.Count, "M").End(xlUp).Row
    Set Ocel = .Range("O1")
    
    With .Columns(MCol)
    For i = 1 To LastRow Step 22
    .Cells(i).Resize(22, 0).Copy
    Ocel.PasteSpecial , , , Transpose
    Set Ocel = Ocel.Offset(1)
    Next i
    End With
    End With
    
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    VBAX Newbie
    Joined
    Nov 2016
    Posts
    5
    Location
    Thank you so much for responding to me so fast!

    When I try to run the macro it gives me "compile error: Variable not defined" and it selects Transpose from this line of code : Ocel.PasteSpecial , , , Transpose . It also highlights Sub Transposer22() after I click okay.

    Do I need to define Transpose up near MCol, Ocel, i, and LastRow? If so then what data type would it be?

    Thanks,
    Kiba

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,875
    Ocel.PasteSpecial , , , Transpose
    should probably be:
    Ocel.PasteSpecial , , , True

    edit post trying it out:
    You can't have .Resize(22,0) - that would be a column zero cells wide, it needs to be
    .Resize(22,1) or more simply .Resize(22) :

    .Cells(i).Resize(22).Copy
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  5. #5
    VBAX Newbie
    Joined
    Nov 2016
    Posts
    5
    Location
    Thank you! Those two bug fixes made the code work properly.

    As a side note, what function does this line of code have: Const MCol As Long = 13 (where does the 13 come from?)

    I'm just wondering in case I need to alter this code in the future / transpose something else that is slightly different.



    Thanks again to both of you for helping me out!

    Kiba

  6. #6
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Column "M" is the 13th column.

    With MCol
    '...
    .Cells(i)
    returns the i'th cell down in column M
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  7. #7
    VBAX Newbie
    Joined
    Nov 2016
    Posts
    5
    Location
    Makes sense. Thanks again!

    I'll mark the thread as solved now!

  8. #8
    I know it has been marked as solved but this might be slightly faster on a large range.
    Sub Kiba()
    Dim lr As Long, j As Long, i As Long
    lr = Cells(Rows.Count, 13).End(xlUp).Row
    j = 1
    For i = 1 To WorksheetFunction.RoundUp(lr / 22, 0)
        Cells(i, 15).Resize(, 22).Value = WorksheetFunction.Transpose(Cells(j, 13).Resize(22))
        j = j + 22
    Next i
    End Sub

  9. #9
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Since I was working on it anyways, just throwing out another option that uses internal arrays for speed to rearrange


    Option Explicit
    
    Sub test()
        Const cSrc As String = "M1"
        Const cDest As String = "O1"
        Const cWidth As Long = 22
        
        Dim rSrc As Range
        Dim V1 As Variant, V2() As Variant
        Dim r As Long, c As Long
        
        Set rSrc = Range(Range(cSrc), Range(cSrc).End(xlDown))
        
        V1 = Application.WorksheetFunction.Transpose(rSrc.Value)
        ReDim Preserve V2(0 To (UBound(V1) / cWidth) - 1, 0 To cWidth - 1)
        
        For r = LBound(V1) - 1 To UBound(V1) - 1
            V2(r \ cWidth, r Mod cWidth) = V1(r + 1)
        Next r
        Range(cDest).Resize(UBound(V2, 1) + 1, UBound(V2, 2) + 1).Value = V2
    
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  10. #10
    @ Paul
    Might need adjusting if the division is not exact.

  11. #11
    VBAX Newbie
    Joined
    Nov 2016
    Posts
    5
    Location
    Wow, this is my first thread on this forum and everyone is so helpful! Thanks Jolivanes and Paul_Hossler for additional solutions!

    Kiba

  12. #12
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Quote Originally Posted by jolivanes View Post
    @ Paul
    Might need adjusting if the division is not exact.

    Oh, there's many other ways it could fail. That's just one

    BTW, if all of the elements are the same and standard VBA variable types (Long, Double, etc.), there's much faster ways than looping in arrays
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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