Sorting Data from Multiple Pages by Date Based on Category and Subcategory

Mlwhiteman

New Member
Joined
Nov 26, 2017
Messages
12
Hello,

I am attempting to generate an organized list of all entries from multiple sheets which have a date, description, cost, category, and subcategory. I am looking to have them sorted by date based upon their category and then their subcategory, and to display the original date, description, and cost of the data which belongs to the particular category and subcategory. I have three (3) worksheets for this example: Worksheet #1 : (Month 1 Summary), Worksheet #2 : (Month 2 Summary), and Worksheet #3 (Category Summary). Worksheets #1 and #2 are how the data is currently organized, and Worksheet #3 is the desired result. I am assuming that the header for "Month" begins in cell B3. Any help would be appreciated. Thanks!

BEFORE (Month 1 Summary)
MonthDayDescriptionAmountCategorySubcategoryNotes
11CVS$5.00Cat 1Subcat 1
11Home Depot$10.00Cat 1Subcat 2
12McDonald's$15.00Cat 1Subcat 3
13CVS$20.00Cat 2Subcat 1
14Home Depot$25.00Cat 2Subcat 2

<colgroup><col><col><col><col><col><col><col></colgroup><tbody>
</tbody>

BEFORE (Month 2 Summary)
MonthDayDescriptionAmountCategorySubcategoryNotes
21CVS$30.00Cat 1Subcat 1
21Home Depot$35.00Cat 1Subcat 2
22McDonald's$40.00Cat 1Subcat 3
23CVS$45.00Cat 2Subcat 1
24Home Depot$50.00Cat 2Subcat 2

<colgroup><col><col><col><col><col><col><col></colgroup><tbody>
</tbody>

AFTER (Category Summary)
DateDescriptionAmount
Cat 1
Subcat 1
1/1CVS$5.00
2/1CVS$30.00
Subcat 2
1/1Home Depot$10.00
2/1Home Depot$35.00
Subcat 3
1/2McDonald's$15.00
2/2McDonald's$40.00
Cat 2
Subcat 1
1/3Shell$20.00
2/3Shell$45.00
Subcat 2
1/4Michael's$25.00
2/4Michael's$50.00

<colgroup><col><col><col><col></colgroup><tbody>
</tbody>
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

MickG

MrExcel MVP
Joined
Jan 9, 2008
Messages
14,841
Try this:-
Data on sheets "Month 1 Summary" & "Month 2 Summary"
Results on sheet "Category Summary", all data starting "A1"

Code:
[COLOR="Navy"]Sub[/COLOR] MG27Nov30
[COLOR="Navy"]Dim[/COLOR] Dic         [COLOR="Navy"]As[/COLOR] Object
[COLOR="Navy"]Dim[/COLOR] Rng1        [COLOR="Navy"]As[/COLOR] Range, Rng2 [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Rng         [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] k           [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Dim[/COLOR] p           [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Dim[/COLOR] G           [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Dim[/COLOR] c           [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Q           [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Dim[/COLOR] R           [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Ac          [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer[/COLOR]
[COLOR="Navy"]Dim[/COLOR] n           [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Dt          [COLOR="Navy"]As[/COLOR] Date
[COLOR="Navy"]Dim[/COLOR] Ray         [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]With[/COLOR] Sheets("Month 1 Summary")
    [COLOR="Navy"]Set[/COLOR] Rng1 = .Range("E2", .Range("E" & Rows.Count).End(xlUp))
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]With[/COLOR] Sheets("Month 2 Summary")
    [COLOR="Navy"]Set[/COLOR] Rng2 = .Range("E2", .Range("E" & Rows.Count).End(xlUp))
[COLOR="Navy"]End[/COLOR] With
Ray = Array(Rng1, Rng2)
[COLOR="Navy"]Set[/COLOR] Dic = CreateObject("Scripting.Dictionary")
    Dic.CompareMode = 1
   [COLOR="Navy"]For[/COLOR] Ac = 0 To 1
    [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Ray(Ac)
    [COLOR="Navy"]If[/COLOR] Not Dic.exists(Dn.Value) [COLOR="Navy"]Then[/COLOR]
            [COLOR="Navy"]Set[/COLOR] Dic(Dn.Value) = CreateObject("Scripting.Dictionary")
        [COLOR="Navy"]End[/COLOR] If
            
            [COLOR="Navy"]If[/COLOR] Not Dic(Dn.Value).exists(Dn.Offset(, 1).Value) [COLOR="Navy"]Then[/COLOR]
                [COLOR="Navy"]Set[/COLOR] Dic(Dn.Value)(Dn.Offset(, 1).Value) = CreateObject("Scripting.Dictionary")
                    Dic(Dn.Value)(Dn.Offset(, 1).Value).CompareMode = 1
            [COLOR="Navy"]End[/COLOR] If
              [COLOR="Navy"]If[/COLOR] Not Dic(Dn.Value)(Dn.Offset(, 1).Value).exists(Dn.Offset(, -2).Value) [COLOR="Navy"]Then[/COLOR]
                   
                    ReDim nRay(1 To Rng1.Count + Rng2.Count, 1 To 3)
                    nRay(1, 1) = Dn.Offset(, -4).Value
                    nRay(1, 2) = Dn.Offset(, -3).Value
                    nRay(1, 3) = Dn.Offset(, -1).Value
                    Dic(Dn.Value)(Dn.Offset(, 1).Value).Add (Dn.Offset(, -2).Value), Array(nRay, 1)
             [COLOR="Navy"]Else[/COLOR]
                Q = Dic(Dn.Value)(Dn.Offset(, 1).Value).Item(Dn.Offset(, -2).Value)
                    Q(1) = Q(1) + 1
                    Q(0)(Q(1), 1) = Dn.Offset(, -4)
                    Q(0)(Q(1), 2) = Dn.Offset(, -3)
                    Q(0)(Q(1), 3) = Dn.Offset(, -1)
                Dic(Dn.Value)(Dn.Offset(, 1).Value).Item(Dn.Offset(, -2).Value) = Q
 
            [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Next[/COLOR] Dn
 [COLOR="Navy"]Next[/COLOR] Ac
c = 1
ReDim Ray(1 To (Rng1.Count + Rng2.Count) * 2, 1 To 4)
Ray(1, 2) = "Date": Ray(1, 3) = "Description": Ray(1, 4) = "Amount"
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] k [COLOR="Navy"]In[/COLOR] Dic.Keys
           c = c + 1
           Ray(c, 1) = k
        [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] p [COLOR="Navy"]In[/COLOR] Dic(k).Keys
                c = c + 1
                Ray(c, 2) = p
            [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] G [COLOR="Navy"]In[/COLOR] Dic(k)(p).Keys
                [COLOR="Navy"]For[/COLOR] R = 1 To Dic(k)(p).Item(G)(1)
                    c = c + 1
                    Dt = DateSerial("2017", Dic(k)(p).Item(G)(0)(R, 1), Dic(k)(p).Item(G)(0)(R, 2))
                    Ray(c, 2) = Format(Dt, "DD_mmm")
                    Ray(c, 3) = G
                    Ray(c, 4) = Dic(k)(p).Item(G)(0)(R, 3)
                [COLOR="Navy"]Next[/COLOR] R
            [COLOR="Navy"]Next[/COLOR] G
        [COLOR="Navy"]Next[/COLOR] p
[COLOR="Navy"]Next[/COLOR] k
[COLOR="Navy"]With[/COLOR] Sheets("Category Summary").Range("A1").Resize(c, 4)
    .Value = Ray
    .Borders.Weight = 2
    .Columns.AutoFit
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 

Mlwhiteman

New Member
Joined
Nov 26, 2017
Messages
12
Hi Mick,

Thanks for the assistance! When I run the macro I get the message "Type mismatch".
 

Mlwhiteman

New Member
Joined
Nov 26, 2017
Messages
12
Hi Mick,

I noticed where the original problem is. I'm still relatively new to VBA. If I want the original table that you posted on sheets "Month 1 Summary" and "Month 2 Summary" to begin on cell E5, what has to change?
 

MickG

MrExcel MVP
Joined
Jan 9, 2008
Messages
14,841
Try changing the lines below to reflect a starting point of "E5"
Code:
[COLOR=#000080]With[/COLOR] Sheets("Month 1 Summary")
    [COLOR=navy]Set[/COLOR] Rng1 = .Range[B][COLOR=#FF0000]("E5", [/COLOR][/B].Range("E" & Rows.Count).End(xlUp))
[COLOR=navy]End[/COLOR] With
[COLOR=navy]With[/COLOR] Sheets("Month 2 Summary")
    [COLOR=navy]Set[/COLOR] Rng2 = .Range[B][COLOR=#FF0000]("E5", [/COLOR][/B].Range("E" & Rows.Count).End(xlUp))
[COLOR=navy]End[/COLOR] With
 

Watch MrExcel Video

Forum statistics

Threads
1,129,349
Messages
5,635,774
Members
416,880
Latest member
kyleswanson

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
Top