+ Reply to Thread
Results 1 to 5 of 5

Tricky Transpose and Sort

Hybrid View

  1. #1
    Registered User
    Join Date
    04-16-2009
    Location
    Carson City, Nevada
    MS-Off Ver
    Excel 2007
    Posts
    16

    Tricky Transpose and Sort

    First, thank you for looking and any assistance you can provide with this data problem. I have a very large spreadsheet consisting of thousands of rows. I need to transpose the data from columns to rows, but it needs to also sort the data into month/year categories. This may best be done in two separate steps. I have attached a file with two sheets. One is an example of the raw data and the second is the desired format. Any assistance is greatly appreciated.
    Thank you
    Attached Files Attached Files
    Last edited by rylo; 11-03-2010 at 05:31 PM.

  2. #2
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Tricky Transpose and Sort

    Hi

    See how this goes.
    Sub aaa()
      Dim WorkSH As Worksheet, DataSH As Worksheet, OutSH As Worksheet
      Set DataSH = Sheets("Raw Data")
      Set OutSH = Sheets("Sheet3")
      
      Set WorkSH = Sheets.Add
      WorkSH.Name = "Worksheet"
      
      DataSH.Activate
      OutSH.Range("A1:G1").Value = DataSH.Range("A1:G1").Value
      Range("J1").Value = "Date2"
      Range("J2").Formula = "=EOMONTH(H2,-1)+1"
      Range("J2").AutoFill Destination:=Range("J2:J" & Cells(Rows.Count, "H").End(xlUp).Row)
      
      DataSH.Range("J:J").AdvancedFilter action:=xlFilterCopy, copytorange:=WorkSH.Range("A1"), unique:=xlYes
      
      WorkSH.Range("A:A").Sort key1:=WorkSH.Range("A1"), order1:=xlAscending, header:=xlYes
      WorkSH.Range("A:A").Replace what:="#NUM!", replacement:=""
      With WorkSH
        .Range(.Range("A2"), .Range("A2").End(xlDown)).Copy
        OutSH.Range("H1").PasteSpecial Paste:=xlPasteAll, Transpose:=True
      End With
      Application.DisplayAlerts = False
      WorkSH.Delete
      Application.DisplayAlerts = True
      
      For Each ce In Range("H2:H" & Cells(Rows.Count, "H").End(xlUp).Row)
        If Not IsEmpty(ce) Then
          If Not IsEmpty(ce.Offset(0, -7)) Then
            outrow = OutSH.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
            OutSH.Cells(outrow, 1).Resize(1, 7).Value = Cells(ce.Row, 1).Resize(1, 7).Value
          End If
          
          outcol = WorksheetFunction.Match(ce.Offset(0, 2), OutSH.Rows("1:1"), 0)
          OutSH.Cells(outrow, outcol).Value = ce.Offset(0, 1).Value
        End If
      
      Next ce
       
      DataSH.Range("J:J").ClearContents
      OutSH.Range("A1").CurrentRegion.EntireColumn.AutoFit
      With OutSH
        For i = .Cells(Rows.Count, 1).End(xlUp).Row To 3 Step -1
          .Cells(i, 1).EntireRow.Insert shift:=xlDown
        Next i
      End With
    End Sub
    rylo
    Last edited by rylo; 11-03-2010 at 01:07 AM. Reason: slight oops in the program

  3. #3
    Registered User
    Join Date
    04-16-2009
    Location
    Carson City, Nevada
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: Tricky Transpose and Sort

    It has been a couple years since I used this code and tried to use it again today with a new set of data and I keep getting an error. I have attached a new sample data set with the macro. Would you please review it and let me know what I have done wrong.
    ThanksData Format-2.xlsm

  4. #4
    Registered User
    Join Date
    04-16-2009
    Location
    Carson City, Nevada
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: Tricky Transpose and Sort

    Rylo,
    I am thanking the Excel gods right now.... it worked perfectly. Thank you so much.

  5. #5
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Tricky Transpose and Sort

    Hi

    You have changed the structure of the file from the original. In the original, the first appearance of the name also had the first appearance of the data. However, in the new file, the first appearance of the data is in the row below the name.

    Restructure your data, and you should be getting somewhere.

    rylo

+ 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