+ Reply to Thread
Results 1 to 5 of 5

Data Sorting Macro - Please help

Hybrid View

  1. #1
    Registered User
    Join Date
    05-02-2012
    Location
    SS
    MS-Off Ver
    Excel 2007
    Posts
    29

    Talking Data Sorting Macro - Please help

    Hello, Im new to macros and I would really appreciate your help.

    I have a problem with a lot of text that can only be imported to excel into one column (A). The only problem is that it needs to be parsed and sorted by several different things. I have found it difficult to find any information on how to modify the information.

    This is how the text looks like in Column A:

    From: 09/01/13 IEX TotalView Detail and Summary
    To: 09/26/13 xxxxxxxx xxxxxxxx S.A. de C.V. Daily
    Shift: 0 All Day Time Utilization Report Page: 4
    MU: 2106 RIM BCTS Sorted by: Name
    Time Zone: America/Costa_Rica
    Report Across Agent Moves: No Report Agent Moves: No
    Sort By Exception Code Group: No
    Show Exception Code Group Totals For Selected Exception Codes: No
    Show Exception Code Details: No
    Duration
    Exception Code HH:MM Percent
    ID Agent Name Date: 09/04/13
    1536 Avalos, Rodolfo
    Client Training 10:00 100.00%
    Total 10:00
    1713 Piedra, Juan
    Client Training 10:00 100.00%
    Total 10:00
    Summary Data For: Date: 09/04/13
    Total Agents: 2
    Client Training 20:00 100.00%
    Total 20:00
    ____________________
    From: 09/01/13 IEX TotalView Detail and Summary
    To: 09/26/13 xxxxxxxx xxxxxxxx S.A. de C.V. Daily
    Shift: 0 All Day Time Utilization Report Page: 5
    MU: 2106 RIM BCTS Sorted by: Name
    Time Zone: America/Costa_Rica
    Report Across Agent Moves: No Report Agent Moves: No
    Sort By Exception Code Group: No
    Show Exception Code Group Totals For Selected Exception Codes: No
    Show Exception Code Details: No
    Duration
    Exception Code HH:MM Percent
    ID Agent Name Date: 09/05/13
    1536 Avalos, Rodolfo
    Client Training 10:00 100.00%
    Total 10:00
    2429 Núñez, Andres
    Client Training 5:15 100.00%
    Total 5:15
    1713 Piedra, Juan
    Client Training 10:00 100.00%
    Total 10:00
    1874 Urena, Carlo
    Client Training 9:00 100.00%
    Total 9:00
    Summary Data For: Date: 09/05/13
    Total Agents: 4
    Client Training 34:15 100.00%
    Total 34:15
    ____________________
    From: 09/01/13 IEX TotalView Detail and Summary
    To: 09/26/13 xxxxxxxx xxxxxxxx S.A. de C.V. Daily
    Shift: 0 All Day Time Utilization Report Page: 6
    MU: 2106 RIM BCTS Sorted by: Name
    Time Zone: America/Costa_Rica
    Report Across Agent Moves: No Report Agent Moves: No
    Sort By Exception Code Group: No
    Show Exception Code Group Totals For Selected Exception Codes: No
    Show Exception Code Details: No
    Duration
    Exception Code HH:MM Percent
    ID Agent Name Date: 09/06/13
    1713 Piedra, Juan
    Client Training 10:00 100.00%
    Total 10:00
    Summary Data For: Date: 09/06/13
    Total Agents: 1
    Client Training 10:00 100.00%
    Total 10:00
    ____________________
    What I need the macro to do is find a way to extract the date, the agents ids (number next to name) in one column, the agent's name in one column, the activity (client training)in one column and the total (10:00) in other column. What is breaking my head is that each agent might have completely different activities and that the list can be as long as 1000 agents

    The sample data can be found on this link https://www.dropbox.com/s/9mxs1wonu5...ro%20Data.xlsx

    Thanks!!!

  2. #2
    Forum Contributor
    Join Date
    09-22-2012
    Location
    Berkeley, CA
    MS-Off Ver
    Excel 2010
    Posts
    112

    Re: Data Sorting Macro - Please help

    Have you tried the "text to columns" tool? I don't know if it will do what you want, but maybe it's worth a shot.
    ------------------------
    W2

  3. #3
    Registered User
    Join Date
    05-02-2012
    Location
    SS
    MS-Off Ver
    Excel 2007
    Posts
    29

    Re: Data Sorting Macro - Please help

    Hi Cofad. Actually I did, but it didnt work

  4. #4
    Forum Contributor
    Join Date
    09-22-2012
    Location
    Berkeley, CA
    MS-Off Ver
    Excel 2010
    Posts
    112

    Re: Data Sorting Macro - Please help

    Seems to me like a good first attempt would be to transpose all the data sets into columns and then to delete the unnecessary columns?

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Data Sorting Macro - Please help

    Hello paconovellino,

    The attached workbook organizes the data by date, ID, agent name, task, and time. The data is then output to Sheet3 starting in row 2. Row 1 has headers. There are no blank rows in data. There is a button at the top of Sheet3 to run the macro.

    The macro is very fast. The average execution time on my computer is 0.21 seconds. Here is the macro code. There are 2 standard VBA modules used in this project.

    Fast_Find Module Macro
    Function FastFind(ByVal FindItem As Variant, ByRef FindRng As Range) As Variant
    
      ' Written: July 17, 2013
      ' Author:  Leith Ross
      ' Summary: Searchs a single column using the MATCH function instead of Range Find method.
      '             This method is much faster and a better choice for data sets of 10,000 rows
      '             or more. Checking the zero subscript of the returned array of cells will
      '             will either be a range, if successful, or empty. The number of matches found
      '             is equal to the UBound of the array + 1. The returned array of cells is a
      '             contiguous 1-D array of the cells that matched the search criteria.
      
      
        Dim cnt         As Long
        Dim Data()      As Variant
        Dim n           As Long
        Dim r           As Variant
        Dim Rng         As Range
        Dim RngEnd      As Range
        Dim Wks         As Worksheet
        
            Set Wks = FindRng.Parent
            
            Set Rng = FindRng
            Set RngEnd = FindRng.Cells(FindRng.Rows.Count + FindRng.Row - 1, 1)
            
                ReDim Data(0 To FindRng.Rows.Count)
                
                For n = r To Rng.Rows.Count
                    r = Application.Match(FindItem, FindRng, 0)
                    If VarType(r) <> vbError Then
                        Set Data(cnt) = FindRng.Cells(r, 1)
                        cnt = cnt + 1
                        
                      ' Shorten the FindRng for the next search.
                        r = r + 1
                        Set FindRng = Wks.Range(FindRng.Cells(r, 1), RngEnd)
                    Else
                        Exit For
                    End If
                Next n
                    
            If cnt Then ReDim Preserve Data(cnt)
            FastFind = Data
        
    End Function
    Module1 Macro
    Sub Macro1()
    
        Dim AgentName   As String
        Dim Cell        As Variant
        Dim DataOut     As Variant
        Dim Dates       As Variant
        Dim DateCell    As Variant
        Dim DateRng     As Range
        Dim Details     As Variant
        Dim EntryDate   As Variant
        Dim i           As Long
        Dim ID          As Double
        Dim Item        As Variant
        Dim n           As Long
        Dim r           As Long
        Dim RegExp      As Object
        Dim Rng         As Range
        Dim RngEnd      As Range
        Dim Wks         As Worksheet
        
            Set Wks = Worksheets("Sheet2")
            
            Set Rng = Wks.Range("A1")
            Set RngEnd = Wks.Cells(Rows.Count, "A").End(xlUp)
            
            Set Rng = Wks.Range(Rng, RngEnd)
            
                Set RegExp = CreateObject("VBScript.RegExp")
                RegExp.Pattern = "^\s+(\d+)\s(.+)"
                
                Dates = FastFind("*ID Agent Name*", Rng)
                   
                ReDim DataOut(1 To RngEnd.Row, 1 To 5)
       
                For i = 0 To UBound(Dates) - 1
                    Set DateCell = Dates(i)
                    EntryDate = Mid(DateCell, 50, 8)
                    EntryDate = Format(EntryDate, "dd/mm/yyyy")
                    
                    If i + 1 < UBound(Dates) Then
                        Set DateRng = DateCell.Resize(Dates(i + 1).Row - DateCell.Row, 1)
                    Else
                        Set DateRng = DateCell.Resize(RngEnd.Row - DateCell.Row, 1)
                    End If
                    
                      ' Find ID numbers and Agent Names for this date.
                        For Each Cell In DateRng
                            If RegExp.Test(Cell) Then
                                ID = Val(RegExp.Replace(Cell, "$1"))
                                AgentName = RTrim(RegExp.Replace(Cell, "$2"))
    
                                r = Cell.End(xlDown).Row
                                
                                If r - Cell.Row = 1 Then
                                    ReDim Details(1 To 1, 1 To 1)
                                    Details(1, 1) = Cell.Offset(1, 0).Value
                                Else
                                    Details = Cell.Offset(1, 0).Resize(r - Cell.Row, 1).Value
                                End If
                                
                                For Each Item In Details
                                    n = n + 1
                                    DataOut(n, 1) = EntryDate
                                    DataOut(n, 2) = ID
                                    DataOut(n, 3) = AgentName
                                    DataOut(n, 4) = RTrim(Mid(Item, 16, 41 - 16))
                                    DataOut(n, 5) = RTrim(Mid(Item, 42, 50 - 42))
                                Next Item
                            End If
                        Next Cell
                Next i
            
          ' Output the data to Sheet3 starting in row 2.
            Sheet3.Range("A2:E2").Resize(n, 5).Value = DataOut
        
    End Sub
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

+ 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. [SOLVED] Sorting Data - copy only needed data (macro)
    By city in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-27-2012, 06:57 AM
  2. Data sorting macro
    By Moyisi in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-02-2010, 05:05 PM
  3. Data Sorting Macro Help
    By camer in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-27-2008, 01:35 AM
  4. sorting data Macro...
    By neilcarden in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-10-2007, 09:09 AM
  5. Macro for sorting color data
    By Tufail in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-04-2006, 08:10 AM

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