Sorting/Consolidating data using VBA -----------------------------------------Second Attempt-----------Please help----------------

sssb2000

Well-known Member
Joined
Aug 17, 2004
Messages
1,169
Hi everyone,
thanks for looking.

i have the above raw data.......i'm trying to get to the output shown below using VBA. There is about 1600 rows of data in my raw data set and they need to be consolidated into the output table shown below.

there needs to be four of these output tables, one for each quarter.......ie. quarter one is jan. 1 to March 31, etc.


Your help is very much appreciated.

RAW Data:

Excel 2010
ANOPQRST
1xxxMaxLocationTitleStatusStart Date/TimeEnd Date/Timexxx
2*********30ACourse #mpkek234CANCELLED12/3/2012 8:0012/3/2012 12:00*********
3*********20ACourse #mpkek234ENROLLED12/3/2012 8:0012/3/2012 12:00*********
4*********40BCourse #mpkek234ENROLLED12/5/2012 8:0012/6/2012 12:00*********
5*********40BCourse #mpkek234ENROLLED12/5/2012 8:0012/6/2012 12:00*********
6*********40BCourse #mpkek234CANCELLED12/5/2012 8:0012/6/2012 12:00*********
7*********40Ccourse 4k5jj3CANCELLED11/5/2012 8:0011/9/2012 17:00*********
8*********40Ccourse 4k5jj3CANCELLED11/5/2012 8:0011/9/2012 17:00*********
9*********40Ccourse 4k5jj3ENROLLED11/5/2012 8:0011/9/2012 17:00*********
10*********40Ccourse 4k5jj3WAITLIST11/5/2012 8:0011/9/2012 17:00*********
11*********25DCourse kk3kjWAITLIST3/12/2013 9:003/14/2013 18:00*********
12*********25DCourse kk3kjWAITLIST3/12/2013 9:003/14/2013 18:00*********
13*********21ECourse kk3kjENROLLED11/6/2012 9:0011/8/2012 18:00*********
14*********21ECourse kk3kjENROLLED11/6/2012 9:0011/8/2012 18:00*********
15*********21ECourse kk3kjENROLLED11/6/2012 9:0011/8/2012 18:00*********
16*********21ECourse kk3kjCANCELLED11/6/2012 9:0011/8/2012 18:00*********
report(3)



Output:


Excel 2010
ABCDEFGHIJKL
2Course NumberLocStart DateEnd dateMaxTotal RegCancelWaitlistCurrently EnrolledNo ShowWalk-inFinal Participants
3Course #mpkek234A12/3/201212/3/2012302101001
4Course #mpkek234B12/5/201212/6/2012203102002
5course 4k5jj3C11/5/201211/9/2012404211001
6Course kk3kjD10/18/201210/19/2012402020000
7Course kk3kjE3/12/20133/14/2013254103003
Q1 FY13
 
Hello sssb2000,

That error tells me your version of Office is not 2007 or 2010. What version are you using?
 
Upvote 0

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Hello sssb2000,

The file you posted seems to be an 95 - 2003 version of Excel. However, when I try to open it Excel tells me there is problem with the file format. Did you change the file extension?
 
Upvote 0
oops. you're right.
I'm at home now and thought i have 2007 but no, i have 2002. good call.
i'll try it first thing in the am at work.

by the way, did you get a chance to take a look at the file i posted in page 1? Could you please take a look and see if the code does all the 'rules' i listed in the "Raw" page?
thank you again and again!
Hello sssb2000,

That error tells me your version of Office is not 2007 or 2010. What version are you using?
 
Upvote 0
No, the file has an .xlsx extension. i made it at work using my MS office 2010
i can still open it using my excel 2002 though.....strange....

Hello sssb2000,

The file you posted seems to be an 95 - 2003 version of Excel. However, when I try to open it Excel tells me there is problem with the file format. Did you change the file extension?
 
Upvote 0
Hello sssb2000,

I was able to open the file on my other computer that has Office 2003. It has a file converter to read Files in 2007 format. However, it will not read on my 2010 computer.

I will need to make some changes to the macro to work the way you descibed. Shouldn't take to much time to do.
 
Upvote 0
I'm glad it worked.
thanks very much. I'll standby.

Hello sssb2000,

I was able to open the file on my other computer that has Office 2003. It has a file converter to read Files in 2007 format. However, it will not read on my 2010 computer.

I will need to make some changes to the macro to work the way you descibed. Shouldn't take to much time to do.
 
Upvote 0
Hello sssb2000,

I had to make some major changes to macro and that of course led to new problems. I have tested the macro thoroughly using the data you provided but the real test will be on the database itself. There were some issues with the status like "WaitList" and "WaitListed". Those have been corrected. The "Pending" status is now counted as "Enrolled". Try this out on your database and let me know what happens. Here is the link to the workbook and the macro code.

Link to the Workbook...
Data Consolidation ver 2.xlsm

Code:
' http://www.mrexcel.com/forum/excel-questions/682241-sorting-consolidating-data-using-visual-basic-applications-second-attempt-please-help.html
' Written: January 30, 2013 for sssb2000
' Updated: January 31, 2013 - Added Buttons to each quarterly sheet that will create a report based on the dates the user selects.
' Author:  Leith Ross

Public RawDataPrepped As Boolean
Public RawDataRng As Range

Sub PrepRawData()

    Dim Cell As Range
    Dim Header As Variant
    Dim Wks As Worksheet
    
        Set Wks = Worksheets("Raw")
        
        Wks.AutoFilterMode = False
        Wks.Sort.SortFields.Clear
        
        For Each Header In Array("Start Date/Time", "End Date/Time")
            Set Cell = Wks.UsedRange.Find(Header, , xlValues, xlWhole, xlByRows, xlNext, False, False, False)
            If Not Cell Is Nothing Then
                Cell = Left(Cell, Len(Cell) - 5)
                Cell.EntireColumn.NumberFormat = "m/d/yyyy"
                Cell.EntireColumn.AutoFit
            End If
        Next Header
        
        Set RawDataRng = Wks.Range("N1:S1")
        Set RngEnd = Wks.Cells(Rows.Count, RawDataRng.Column).End(xlUp)
        If RngEnd.Row < RawDataRng.Row Then Exit Sub Else Set RawDataRng = RawDataRng.Resize(RowSize:=RngEnd.Row - RawDataRng.Row + 1)
        
      ' Sort the Raw data by location and date.
        RawDataRng.Sort _
            Key1:=RawDataRng.Cells(1, 3), Order1:=xlAscending, _
            Key2:=RawDataRng.Cells(1, 6), Order2:=xlAscending, _
            Header:=xlYes, MatchCase:=False, _
            Orientation:=xlTopToBottom, _
            DataOption1:=xlSortTextAsNumbers, _
            DataOption2:=xlSortTextAsNumbers
        
        Wks.Sort.Apply
        
        RawDataPrepped = True
        
End Sub

Sub CreateReport()

    Dim Course As String
    Dim Data As Variant
    Dim DataRng As Range
    Dim Dict As Object
    Dim EndDate As Date
    Dim Location As Variant
    Dim Qfilter As Integer
    Dim r As Long
    Dim Rng As Range
    Dim rngArea As Range
    Dim RngEnd As Range
    Dim StartDate As Date
    Dim Status As String
    Dim Wks As Worksheet
    Dim wksReport As Worksheet
    
        Set Wks = Worksheets("Raw")
        Set wksReport = ActiveSheet
        
        If Not RawDataPrepped Then Call PrepRawData
        
        If RawDataRng Is Nothing Then
            MsgBox "There is No Raw Data Available.", vbExclamation
            Exit Sub
        End If
        
        Application.ScreenUpdating = False
               
          ' Filter the data using the Start and End Dates.
            If wksReport.Range("B2") = "" Or wksReport.Range("C2") = "" Then
                MsgBox "Please fill in the Start and End dates.", vbExclamation
                Exit Sub
            End If
            
                StartDate = wksReport.Range("B2")
                EndDate = wksReport.Range("C2")
                
              ' Modify the data for filtering.
                wksReport.Range("B2") = ">=" & StartDate
                wksReport.Range("C2") = "<=" & EndDate
            
              ' Filter the data. The Criteria range must be a Named Range!
                If Wks.FilterMode Then Wks.ShowAllData
                RawDataRng.AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range(wksReport.Name & "_Criteria")
                
              ' Restore the dates as they were.
                wksReport.Range("B2") = StartDate
                wksReport.Range("C2") = EndDate
        
              ' Get the filtered cells less the header row.
                Set DataRng = RawDataRng.SpecialCells(xlCellTypeVisible)
                Set DataRng = Intersect(DataRng, Wks.Range("N2", "S" & Rows.Count))
                
              ' Exit if there is no filtered data.
                If DataRng Is Nothing Then
                    wksReport.UsedRange.Offset(1, 0).ClearContents
                    wksReport.Activate
                    Application.ScreenUpdating = True
                    Exit Sub
                End If
            
                ReDim Data(11)
                
                  ' Start consolidating the data.
              
                    Set Dict = CreateObject("Scripting.Dictionary")
                    Dict.CompareMode = vbTextCompare
                
                    For Each rngArea In DataRng.Areas
                        For Each Cell In rngArea.Columns(4).Cells
                            Status = UCase(Trim(Cell))
                    
                            If Status <> "" Then
                                Location = Cell.Offset(0, -2) & Cell.Offset(0, 1)
                        
                                If Not Dict.Exists(Location) Then
                                    ReDim Data(11)
                                    Data(0) = Cell.Offset(0, -1).Value  ' Course
                                    Data(1) = Cell.Offset(0, -2).Value  ' Location
                                    Data(2) = Cell.Offset(0, 1).Value   ' Start Date
                                    Data(3) = Cell.Offset(0, 2).Value   ' End Date
                                    Data(4) = Cell.Offset(0, -3).Value  ' Max Registration
                                    Data(8) = 0                         ' Currently Enrolled
                                    Dict.Add Location, Data
                                End If
                        
                                Data = Dict(Location)
                            
                                Select Case Status
                                    Case Is = "ENROLLED", "PENDING":    Data(8) = Data(8) + 1
                                    Case Is = "CANCEL", "CANCELLED":    Data(6) = Data(6) + 1
                                    Case Is = "WAITLIST", "WAITLISTED": Data(7) = Data(7) + 1
                                    Case Is = "WALK-IN":                Data(10) = Data(10) + 1
                                    Case Is = "NO SHOW":                Data(9) = Data(9) + 1
                                End Select
                        
                                Data(5) = Data(5) + 1                   ' Total Registered
                                Data(11) = Data(8) - Data(9) + Data(10) ' Final Participants
                        
                                Dict(Location) = Data
                            End If
                        Next Cell
                    Next rngArea
                    
              ' Copy the consolidateed data to the Report worksheet.
                Set Rng = wksReport.Range("A5").Resize(columnSize:=UBound(Data) + 1)
                Rng.CurrentRegion.Offset(1, 0).ClearContents
                
                For Each Location In Dict
                    Rng.Offset(r, 0).Value = Dict(Location)
                    r = r + 1
                Next Location
            
            Application.ScreenUpdating = True
        
        wksReport.Activate
        
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top