+ Reply to Thread
Results 1 to 14 of 14

Sorting export Data

Hybrid View

  1. #1
    Registered User
    Join Date
    05-29-2015
    Location
    Stoke-On-Trent, England
    MS-Off Ver
    2010
    Posts
    40

    Sorting export Data

    Hi All

    I have an export from a system that I need to sort so that it is easier to work with.

    I would like to do this through VBA. I have attached a copy of a sample file. Sheet 1 is how the report will export. Sheet 2 is the ideal output of the report.

    Also I have a couple of considerations to take in to account and this is where I am getting stuck:

    1 - The team size of the team will change depending on which team I have exported the data for. This would mean that the ranges would need to be dynamic. Employee will always appear to the left of a date and above the peoples names.

    2 - The Labour Number is not an individual reference instead this links people back to a Department & Area

    3 - any field which contains data like "M 8:00" would need to be changed to something such as "MAT/PAT"

    That should pretty much cover it all but if you do have any questions please feel free to drop me a line I will be more than happy to help

    Thank you in advance for any support that you may be able to give

    D
    Attached Files Attached Files

  2. #2
    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

    There is no Sheet 2...?
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  3. #3
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,264

    Re: Sorting export Data

    Your example workbook only has one sheet, so there is no 'ideal' sorting to view
    Bernie Deitrick
    Excel MVP 2000-2010

  4. #4
    Registered User
    Join Date
    05-29-2015
    Location
    Stoke-On-Trent, England
    MS-Off Ver
    2010
    Posts
    40

    Re: Sorting export Data

    Quote Originally Posted by Bernie Deitrick View Post
    Your example workbook only has one sheet, so there is no 'ideal' sorting to view
    Hi thanks for the quick response I uploaded the attachment before I had saved the changes to the work book .

    Here is I have now added the correct work book to this post.

    Hope that this helps more than the last one did
    Attached Files Attached Files

  5. #5
    Valued Forum Contributor ranman256's Avatar
    Join Date
    07-29-2012
    Location
    Kentucky
    MS-Off Ver
    Excel 2003
    Posts
    1,189

    Re: Sorting export Data

    this collates the data, then you can remove the unwanted EMPLOYEE recs, zero work,etc
    rename M8:00
    and parse the WorkTime into StartTime and EndTime

    run: aParseTime

    Sub aParseTime()
    Dim vRowdate, vWord, vDat
    Dim iRows As Long, iCols As Long, c As Long, iOff As Long
    Dim shtSrc As Worksheet, shtTarg As Worksheet
    Dim sColLtr As String
    
    Set shtSrc = ActiveSheet
    Range("B1").Select
    FarDown
    iRows = ActiveCell.Row
    
    'INSERT 1 COL. A
    Columns("A:A").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("B2").Select
    iCols = ActiveSheet.UsedRange.Columns.Count
    Range("A1").Select
    SetHdrs
      
      'new results sheet
    Sheets.Add
    Set shtTarg = ActiveSheet
    SetHdrs
    shtSrc.Activate
    
    Range("C2").Select  'start at EMP NAME column
    For c = 4 To iCols
        iOff = c - 3
        sColLtr = getColLtr(Cells(2, c))
        Range("C2").Select
        While ActiveCell.Value <> ""
           vWord = ActiveCell.Offset(0, iOff).Value
           vDat = HasDoW(vWord)
           If vDat <> "" Then vRowdate = vDat
           
           ActiveCell.Offset(0, -2).Value = vRowdate
           NextRow
        Wend
    
         Columns(sColLtr & ":" & sColLtr).Copy
         'Range(sColLtr & "2:" & sColLtr & iRows).Copy
        Range("B1").Select
        PasteIt
        
           'copy 3 columns
        Range("A2:C" & iRows).Copy
        shtTarg.Activate
        Range("A1").Select
        FindNextFreeRec
        PasteIt
    
        'clear the source fields
        shtSrc.Activate
        Columns("A:B").Select
        Selection.ClearContents
        Range("A1").Select
    Next   'repeat
    
      'delete all EMPLOYEE recs
    shtTarg.Activate
    SortResults
    
    Set shtSrc = Nothing
    Set shtTarg = Nothing
    End Sub
    
    
    Private Sub NextRow()
    ActiveCell.Offset(1, 0).Select   'next row
    End Sub
    
    Private Sub FarDown()
        Selection.End(xlDown).Select
    End Sub
    
    
    
    Private Function HasDoW(ByVal pvWord)
    Dim vDow, vDat
    vDow = Left(pvWord, 3)
    vDat = Mid(pvWord, 4)
    
    Select Case vDow
      Case "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"
         HasDoW = vDat
      Case Else
         HasDoW = ""
    End Select
    End Function
    
    
    Private Sub SetHdrs()
    Range("A1").Value = "WorkDate"
    Range("B1").Value = "TimeWorked"
    Range("C1").Value = "EMP"
    End Sub
    
    
    Private Sub FindNextFreeRec()
    Range("A1").Select
    Select Case True
       Case ActiveCell.Value = ""
       Case ActiveCell.Offset(1, 0).Value = ""
            NextRow
       Case Else
            FarDown
            NextRow
    End Select
    End Sub
    
    Private Sub PasteIt()
        ActiveSheet.Paste
        Application.CutCopyMode = False
    End Sub
    
    Public Function getColLtr(prng As Range)
    Dim i As Integer
    Dim vAddr
    
    vAddr = prng.Address
    i = InStrRev(vAddr, "$")
    getColLtr = Mid(vAddr, 2, i - 2)
    End Function
       
    Sub SortResults()
    
        Columns("A:C").Select
        ActiveWorkbook.Worksheets("Sheet5").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Sheet5").Sort.SortFields.Add Key:=Range("A2:A5000" _
            ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("Sheet5").Sort
            .SetRange Range("A1:C5000")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        Selection.AutoFilter
        ActiveSheet.Range("$A$1:$C$5000").AutoFilter Field:=3, Criteria1:="Employee"
        Cells.Select
        Selection.ClearContents
        Range("A1").Select
        ActiveCell.FormulaR1C1 = "workdate"
        Range("B1").Select
        ActiveCell.FormulaR1C1 = "time"
        Range("C1").Select
        ActiveCell.FormulaR1C1 = "emp"
        Range("A2").Select
    End Sub

  6. #6
    Registered User
    Join Date
    05-29-2015
    Location
    Stoke-On-Trent, England
    MS-Off Ver
    2010
    Posts
    40

    Re: Sorting export Data

    Thank you for the quick response I have ran updated the macro in the workbook and I am just left with a number of worksheets with no shift information on?

    Would it be possible for you to offer any further guidance at all?
    Last edited by AliGW; 04-03-2018 at 07:55 AM.

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

  8. #8
    Registered User
    Join Date
    05-29-2015
    Location
    Stoke-On-Trent, England
    MS-Off Ver
    2010
    Posts
    40

    Re: Sorting export Data

    Hi

    Thank you for the reply again.

    I have ran the VBA version which is my prefered method which worked great for a small team. When I try to upscale this to a much larger team the dates are not placed in the top row? on the first loop they get placed in row 28? They are also not consistently separated?

    Do you know how I can upscale the macro to work for teams that consist of over 200 people?

    Any further help that you can give would be really appreciated.

    Thanks again

    D
    Last edited by AliGW; 04-03-2018 at 07:55 AM.

  9. #9
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2503 (Windows 11 Home 24H2 64-bit)
    Posts
    90,368

    Re: Sorting export Data

    Please don't quote whole posts, especially when you are responding to the one immediately preceding your own - it's just clutter. It's OK to quote if you are responding to a post out of sequence, but limit quoted content to a few relevant lines that makes clear to whom and what you are responding. Thanks!

    For normal conversational replies, try using the QUICK REPLY box below.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Sorting export Data

    Sub test()
        Dim a, i As Long, ii As Long, n As Long, AL As Object
        Dim myDate As Date
        Set AL = CreateObject("System.Collections.ArrayList")
        With Sheets("sheet1")
            a = .Range("b2", .Cells.SpecialCells(11)).Value
        End With
        With CreateObject("Scripting.Dictionary")
            .CompareMode = 1
            For i = 1 To UBound(a, 1)
                If a(i, 1) = "Employee" Then
                    For ii = 2 To UBound(a, 2)
                        If a(i, ii) Like "*##/##/####" Then
                            a(i, ii) = Replace(a(i, ii), vbLf, "")
                            a(i, ii) = DateSerial(Val(Right$(a(i, ii), 4)), _
                            Val(Mid$(a(i, ii), 7, 2)), Val(Mid$(a(i, ii), 4, 2)))
                        End If
                    Next
                    n = i
                Else
                    If a(i, 1) <> "" Then
                        If Not .exists(a(i, 1)) Then
                            Set .Item(a(i, 1)) = CreateObject("Scripting.Dictionary")
                        End If
                        For ii = 2 To UBound(a, 2)
                            If IsDate(a(n, ii)) Then
                                If Not AL.Contains(a(n, ii)) Then AL.Add a(n, ii)
                                .Item(a(i, 1))(a(n, ii)) = a(i, ii)
                            End If
                        Next
                    End If
                End If
            Next
            ReDim a(1 To .Count + 2, 1 To AL.Count + 1): AL.Sort
            For ii = 0 To AL.Count - 1
                a(1, ii + 2) = AL(ii): a(2, ii + 2) = AL(ii)
            Next
            For i = 0 To .Count - 1
                a(i + 3, 1) = .keys()(i)
                For ii = 2 To UBound(a, 2)
                    a(i + 3, ii) = .items()(i)(a(1, ii))
                Next
            Next
        End With
        With Sheets.Add.[b1].Resize(UBound(a, 1), UBound(a, 2))
            .Value = a
            .Offset(, 1).Resize(2, .Columns.Count - 1).BorderAround Weight:=2
            .Rows(1).NumberFormat = "yyyy/m/d"
            .Rows(2).NumberFormat = "ddd""""dd/mm/yyyy"
            .Columns(1).Offset(2).Resize(.Rows.Count - 2).Borders.Weight = 2
            .WrapText = False
            .Columns.AutoFit
            .Rows.AutoFit
            .Offset(, 1).HorizontalAlignment = xlCenter
            With .Offset(2, 1).Resize(.Rows.Count - 2, .Columns.Count - 1)
                .Borders(11).LineStyle = xlDot
                .Borders(12).Weight = 2
                .BorderAround Weight:=2
            End With
            For ii = 2 To .Columns.Count Step 7
                .Columns(ii).Borders(7).LineStyle = 1
            Next
        End With
    End Sub
    Last edited by jindon; 04-03-2018 at 08:52 AM.

  11. #11
    Registered User
    Join Date
    05-29-2015
    Location
    Stoke-On-Trent, England
    MS-Off Ver
    2010
    Posts
    40

    Re: Sorting export Data

    Hi jindon

    Thanks for that, that is amazing.

    Is there a way of setting the output to be on the same sheet every time at all?

    Thank you again

    D

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Sorting export Data

    Add sheet named "Sheet2" if not in the workbook.
    Sub test()
        Dim a, i As Long, ii As Long, n As Long, AL As Object
        Dim myDate As Date
        Set AL = CreateObject("System.Collections.ArrayList")
        If Not [isref('sheet2'!a1)] Then Sheets.Add.Name = "Sheet2"
        With Sheets("sheet1")
            a = .Range("b2", .Cells.SpecialCells(11)).Value
        End With
        With CreateObject("Scripting.Dictionary")
            .CompareMode = 1
            For i = 1 To UBound(a, 1)
                If a(i, 1) = "Employee" Then
                    For ii = 2 To UBound(a, 2)
                        If a(i, ii) Like "*##/##/####" Then
                            a(i, ii) = Replace(a(i, ii), vbLf, "")
                            a(i, ii) = DateSerial(Val(Right$(a(i, ii), 4)), _
                            Val(Mid$(a(i, ii), 7, 2)), Val(Mid$(a(i, ii), 4, 2)))
                        End If
                    Next
                    n = i
                Else
                    If a(i, 1) <> "" Then
                        If Not .exists(a(i, 1)) Then
                            Set .Item(a(i, 1)) = CreateObject("Scripting.Dictionary")
                        End If
                        For ii = 2 To UBound(a, 2)
                            If IsDate(a(n, ii)) Then
                                If Not AL.Contains(a(n, ii)) Then AL.Add a(n, ii)
                                .Item(a(i, 1))(a(n, ii)) = a(i, ii)
                            End If
                        Next
                    End If
                End If
            Next
            ReDim a(1 To .Count + 2, 1 To AL.Count + 1): AL.Sort
            For ii = 0 To AL.Count - 1
                a(1, ii + 2) = AL(ii): a(2, ii + 2) = AL(ii)
            Next
            For i = 0 To .Count - 1
                a(i + 3, 1) = .keys()(i)
                For ii = 2 To UBound(a, 2)
                    a(i + 3, ii) = .items()(i)(a(1, ii))
                Next
            Next
        End With
        With Sheets("sheet2").[b1].Resize(UBound(a, 1), UBound(a, 2))
            .Parent.Cells.Clear
            .Value = a
            .Offset(, 1).Resize(2, .Columns.Count - 1).BorderAround Weight:=2
            .Rows(1).NumberFormat = "yyyy/m/d"
            .Rows(2).NumberFormat = "ddd""""dd/mm/yyyy"
            .Columns(1).Offset(2).Resize(.Rows.Count - 2).Borders.Weight = 2
            .WrapText = False
            .Columns.AutoFit
            .Rows.AutoFit
            .Offset(, 1).HorizontalAlignment = xlCenter
            With .Offset(2, 1).Resize(.Rows.Count - 2, .Columns.Count - 1)
                .Borders(11).LineStyle = xlDot
                .Borders(12).Weight = 2
                .BorderAround Weight:=2
            End With
            For ii = 2 To .Columns.Count Step 7
                .Columns(ii).Borders(7).LineStyle = 1
            Next
        End With
    End Sub

  13. #13
    Registered User
    Join Date
    05-29-2015
    Location
    Stoke-On-Trent, England
    MS-Off Ver
    2010
    Posts
    40

    Re: Sorting export Data

    Hi Again.

    That is great and is working Fantastically. I have just noticed that some people have 2 lines of data. This is due to the way the information is exported by the systems (which can not be changed). is there a way of amending the VBA so that it does one of the 2 options below:

    1. Only pulls out the pay codes for example "Hol 8:00, .Ho 4:00, Dec 8:00" and so on

    2. Populate 2 worksheets with the lines of data. in worksheet one you would have the full day scheduled events in the second sheet you would have the partial/additional scheduled events.

    I know that the above is a little confusing so if you have any questions or require a sample of data please let me know

    Thank you again for all of the support with this it really is appreciated!

    Thanks

    D

  14. #14
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Sorting export Data

    Change
                        For ii = 2 To UBound(a, 2)
                            If IsDate(a(n, ii)) Then
                                If Not AL.Contains(a(n, ii)) Then AL.Add a(n, ii)
                                .Item(a(i, 1))(a(n, ii)) = a(i, ii)
                            End If
                        Next
    to
                        For ii = 2 To UBound(a, 2)
                            If IsDate(a(n, ii)) Then
                                If Not AL.Contains(a(n, ii)) Then AL.Add a(n, ii)
                                .Item(a(i, 1))(a(n, ii)) = .Item(a(i, 1))(a(n, ii)) & _
                                IIf(.Item(a(i, 1))(a(n, ii)) <> "", ", ", "") & a(i, ii)
                            End If
                        Next

+ Reply to Thread

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