+ Reply to Thread
Results 1 to 7 of 7

vertical transposition macro

Hybrid View

  1. #1
    Registered User
    Join Date
    08-19-2010
    Location
    new york
    MS-Off Ver
    Excel 2010
    Posts
    97

    vertical transposition macro

    ok, so i have what i believe to be the mother of all transposition problems. included is an example. basically, i need all the data from AG:AX to transpose vertically, but it also needs to link to ranges defined in columns F, G and I. Attached is what the columns will always look like and where the data will always be.

    I want a macro, and at the front of the macro should be a code to convert all text to numbers otherwise the info will not feed to the final pivot table.

    I had some theories about groupings and arrays, but i'm worried that the formulas will slow the sheet down as the files can often be over 40,000 lines.

    the tricky part is that the data that links the info, (i.e columns F, G and I) will need to copy and paste itself down to line up with columns AG:AX once they are transposed.

    Make sense?
    Attached Files Attached Files
    Last edited by juniperjacobs; 01-21-2011 at 02:06 PM. Reason: Solved!

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: vertical transposition macro

    Try this:
    Option Explicit
    
    Sub TransposeWIP()
    Dim Sizes As Range, Itms As Range
    Dim SzRws As Long, Rw As Long
    
    'Setup
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    'clear output sheet if it exists
    If Evaluate("ISREF(Results!A1)") Then Sheets("Results").Delete
    
    'duplicate activesheet to create new output sheet
    ActiveSheet.Copy After:=Sheets(Sheets.Count)
    ActiveSheet.Name = "Results"
    
    'insert new columns and clear WIP totals
    Range("AM:AN").Insert xlShiftToRight
    On Error Resume Next
    Range("AL:AL").SpecialCells(xlConstants, xlNumbers).ClearContents
    On Error GoTo 0
    
    'Sizes to copy/transpose
    Set Sizes = Range("AP2:AY2")
    SzRws = 10
    
    'Rows to process
    Set Itms = Range("F:F").SpecialCells(xlConstants, xlNumbers)
    
    'Loop each row from the bottom up inserting values needed
    For Rw = Itms.Rows.Count + 2 To 3 Step -1
        Range("A" & Rw).Offset(1).Resize(SzRws - 1).EntireRow.Insert xlShiftDown
        Range("AM" & Rw).Resize(SzRws).Value = _
            Application.WorksheetFunction.Transpose(Sizes)
        Range("AN" & Rw).Resize(SzRws).Value = _
            Application.WorksheetFunction.Transpose(Range("AP" & Rw, "AY" & Rw))
        Range("E" & Rw, "G" & Rw).Resize(SzRws).Value = _
            Range("E" & Rw, "G" & Rw).Value
        Range("AG" & Rw, "AK" & Rw).Resize(SzRws).Value = _
            Range("AG" & Rw, "AK" & Rw).Value
    Next Rw
    
    'Cleanup
    Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files
    _________________
    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
    08-19-2010
    Location
    new york
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: vertical transposition macro

    look at your never ceasing to amaze! it's working except it's not auto filling column I after the transposition, but it's doing it for F and G. I was trying to find that part in the code to tell it myself but i'm not seeing it. Sorry to be so daft.

  4. #4
    Registered User
    Join Date
    08-19-2010
    Location
    new york
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: vertical transposition macro

    also, i just ran it, and i think my example wasn't clear, it was for one style as a test, but in reality, the sheet i'll be working with will have hundreds of styles so the macro will need to read the whole worksheet. i'm attached a more flushed out example for your ref. you'll notice the columns are a little different, i revised the code to work with this report... which will be the report format used from now on.

    
    Sub TransposeWIP()
    Dim Sizes As Range, Itms As Range
    Dim SzRws As Long, Rw As Long
    
    'Setup
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    'clear output sheet if it exists
    If Evaluate("ISREF(Results!A1)") Then Sheets("Results").Delete
    
    'duplicate activesheet to create new output sheet
    ActiveSheet.Copy After:=Sheets(Sheets.Count)
    ActiveSheet.Name = "Results"
    
    'insert new columns and clear WIP totals
    Range("R:S").Insert xlShiftToRight
    On Error Resume Next
    Range("P:P").SpecialCells(xlConstants, xlNumbers).ClearContents
    On Error GoTo 0
    
    'Sizes to copy/transpose
    Set Sizes = Range("T2:AE2")
    SzRws = 12
    
    'Rows to process
    Set Itms = Range("L:L").SpecialCells(xlConstants, xlNumbers)
    
    'Loop each row from the bottom up inserting values needed
    For Rw = Itms.Rows.Count + 2 To 3 Step -1
        Range("A" & Rw).Offset(1).Resize(SzRws - 1).EntireRow.Insert xlShiftDown
        Range("R" & Rw).Resize(SzRws).Value = _
            Application.WorksheetFunction.Transpose(Sizes)
        Range("S" & Rw).Resize(SzRws).Value = _
            Application.WorksheetFunction.Transpose(Range("T" & Rw, "AE" & Rw))
        Range("B" & Rw, "M" & Rw).Resize(SzRws).Value = _
            Range("B" & Rw, "M" & Rw).Value
        Range("G" & Rw, "K" & Rw).Resize(SzRws).Value = _
            Range("G" & Rw, "K" & Rw).Value
    Next Rw
    
    'Cleanup
    Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    08-19-2010
    Location
    new york
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: vertical transposition macro

    figured this part out: look at your never ceasing to amaze! it's working except it's not auto filling column I after the transposition, but it's doing it for F and G. I was trying to find that part in the code to tell it myself but i'm not seeing it. Sorry to be so daft.

    So, i just need the code to loop through the whole worksheet.

  6. #6
    Registered User
    Join Date
    08-19-2010
    Location
    new york
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: vertical transposition macro

    never mind, solved, it stops when it sees blanks, i get it now, thanks so much for your help!

  7. #7
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: vertical transposition macro

    Glad to help! Looks like you ran most of the way on your own, awesome.

+ Reply to 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.6.0 RC 1