Results 1 to 14 of 14

Sorting export Data

Threaded View

  1. #7
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Sorting export Data

    You can do it with Power Query:

    fnTransform:
    (RemoveTop, KeepTop) =>
    let
        Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
        RemoveTopRows = Table.Skip(Source,RemoveTop),
        KeepTopRows = Table.FirstN(RemoveTopRows,KeepTop),
        PromoteHeaders = Table.PromoteHeaders(KeepTopRows, [PromoteAllScalars=true]),
        Unpivoted = Table.UnpivotOtherColumns(PromoteHeaders, {"Employee"}, "Date", "Value")
    in
        Unpivoted
    Output:
    let
        Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
        #"Removed Other Columns" = Table.SelectColumns(Source,{"Column1"}),
        #"Added RemoveTop" = Table.AddIndexColumn(#"Removed Other Columns", "RemoveTop", 0, 1),
        #"Filtered Employee" = Table.SelectRows(#"Added RemoveTop", each ([Column1] = "Employee")),
        #"Removed Column1" = Table.RemoveColumns(#"Filtered Employee",{"Column1"}),
        #"Added KeepTop" = Table.AddColumn(#"Removed Column1", "KeepTop", each Table.RowCount(Source) / Table.RowCount(#"Removed Column1")),
        Transform = Table.AddColumn(#"Added KeepTop", "Custom", each fnTransform([RemoveTop], [KeepTop])),
        CombineOutput = Table.Combine(Transform[Custom]),
        #"Pivoted Column" = Table.Pivot(CombineOutput, List.Distinct(CombineOutput[Date]), "Date", "Value")
    in
        #"Pivoted Column"




    Or, with VBA:
    Sub TransformData()
        Dim wsSrc As Worksheet
        Dim wsTgt As Worksheet
        
        Dim l As Long
        Dim lRows As Long
        Dim lCols As Long
        
        Set wsSrc = Sheet1
        Set wsTgt = Worksheets.Add
        
        On Error GoTo Terminate
        
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
        
        With wsSrc
            
            lRows = Intersect(.UsedRange, .Columns("B")).Cells.Count
            lRows = lRows / WorksheetFunction.CountIf(.Columns("B"), "Employee")
            lCols = .Cells(2, Columns.Count).End(xlToLeft).Column - 2
            l = 2
            .Cells(l, 2).Resize(lRows, 1).Copy wsTgt.Cells(1, 2)
            Do Until .Cells(l, 2).Value = ""
                .Cells(l, 3).Resize(lRows, lCols).Copy _
                    wsTgt.Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1)
                l = l + lRows
            Loop
        End With
    
    Terminate:
        If Err Then
            Debug.Print "Error", Err.Number, Err.Description
            Err.Clear
        End If
        With Application
            .EnableEvents = True
            .ScreenUpdating = True
        End With
        
    End Sub
    Sample workbooks for both approaches are attached
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 0
    Last Post: 11-10-2016, 10:18 AM
  2. Replies: 1
    Last Post: 12-16-2015, 01:04 PM
  3. Replies: 0
    Last Post: 12-02-2014, 05:02 PM
  4. Replies: 0
    Last Post: 02-24-2014, 11:27 AM
  5. Sorting 2 data ranges by comparing one column in each and sorting to match
    By MDKsmiffy in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 06-17-2013, 03:30 PM
  6. I would need a macro to export data from base example workbook to export worbook
    By slato8 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-01-2012, 11:21 AM
  7. Replies: 0
    Last Post: 10-14-2010, 08:22 AM

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