Summarise data recorded by date in date ranges

lukeh88

New Member
Joined
Nov 8, 2018
Messages
4
Hi - I've been madly googling this problem and cannot find anything that quite solves it for me. Looking to this forum which has helped me so many times before (although first time posting a question!).

I have data capturing number of hours and pay rate, by person, each day. What I want to do is summarise by each person, with a date range. This will be for thousands of people, so obviously open to VBA.
Example of the data;

1/01/20182/01/20183/01/20184/01/20185/01/20186/01/20187/01/20188/01/20189/01/201810/01/2018
Person 14.54.54.54.5555555
Person 118.1018.1018.1019.0019.0019.0019.0019.5019.5019.50
Person 24.54.54.55555555
Person 218.5018.5019.0019.0019.0021.5021.5021.5021.5021.50

<tbody>
</tbody>

Ideal end state;
StartEndHoursRate
Person 11/01/20183/01/20184.518.10
Person 14/01/20184/01/20184.519.00
Person 15/01/20187/01/2018519.00
Person 18/01/201810/01/2018519.50
Person 21/01/20182/01/20184.518.50
Person 23/01/20183/01/20184.519.00
Person 24/01/20185/01/2018519.00
Person 26/01/201810/01/2018521.50

<tbody>
</tbody>


Would really appreciate any assistance here.

Thanks
Luke
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Try this , Result on sheet2.
Code:
[COLOR="Navy"]Sub[/COLOR] MG08Nov19
[COLOR="Navy"]Dim[/COLOR] Ray         [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Dim[/COLOR] Ac          [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] n           [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Dic         [COLOR="Navy"]As[/COLOR] Object
[COLOR="Navy"]Dim[/COLOR] Q           [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Dim[/COLOR] Txt         [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Dim[/COLOR] k           [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Dim[/COLOR] p           [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Dim[/COLOR] c           [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]

Ray = ActiveSheet.Cells(1).CurrentRegion
 [COLOR="Navy"]Set[/COLOR] Dic = CreateObject("Scripting.Dictionary")
    Dic.CompareMode = 1
   
   [COLOR="Navy"]For[/COLOR] n = 2 To UBound(Ray, 1) [COLOR="Navy"]Step[/COLOR] 2
        [COLOR="Navy"]For[/COLOR] Ac = 2 To UBound(Ray, 2)
            Txt = Ray(n, Ac) & "," & Ray(n + 1, Ac)
            [COLOR="Navy"]If[/COLOR] Not Dic.exists(Ray(n, 1)) [COLOR="Navy"]Then[/COLOR]
                [COLOR="Navy"]Set[/COLOR] Dic(Ray(n, 1)) = CreateObject("Scripting.Dictionary")
            [COLOR="Navy"]End[/COLOR] If
        
        [COLOR="Navy"]If[/COLOR] Not Dic(Ray(n, 1)).exists(Txt) [COLOR="Navy"]Then[/COLOR]
                Dic(Ray(n, 1)).Add (Txt), Array(Ray(1, Ac), Ray(1, Ac))
        [COLOR="Navy"]Else[/COLOR]
            Q = Dic(Ray(n, 1)).Item(Txt)
                Q(1) = Ray(1, Ac)
            Dic(Ray(n, 1)).Item(Txt) = Q
        [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Next[/COLOR] Ac
    [COLOR="Navy"]Next[/COLOR] n
   
   ReDim nray(1 To UBound(Ray, 1) * UBound(Ray, 2), 1 To 5)
   nray(1, 1) = "Person": nray(1, 2) = "Start": nray(1, 3) = "End"
   nray(1, 4) = "Hours": nray(1, 5) = "Rate"
   c = 1
    
    [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] k [COLOR="Navy"]In[/COLOR] Dic.Keys
        [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] p [COLOR="Navy"]In[/COLOR] Dic(k)
             c = c + 1
             nray(c, 1) = k
             nray(c, 2) = Dic(k).Item(p)(0)
             nray(c, 3) = Dic(k).Item(p)(1)
             nray(c, 4) = Split(p, ",")(0)
             nray(c, 5) = Split(p, ",")(1)
        [COLOR="Navy"]Next[/COLOR] p
    [COLOR="Navy"]Next[/COLOR] k
[COLOR="Navy"]With[/COLOR] Sheets("Sheet2").Range("A1").Resize(c, 5)
    .Value = nray
    .Columns.AutoFit
    .Borders.Weight = 2
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Thanks Mick,

This works brilliantly. Without realising, additional data points will now come through to add to my problem. I'll give it a try myself but would very much appreciate your guidance, if I were to have another line of data (ie. sales units), what would I need to add to the code?

Try this , Result on sheet2.
Code:
[COLOR=Navy]Sub[/COLOR] MG08Nov19
[COLOR=Navy]Dim[/COLOR] Ray         [COLOR=Navy]As[/COLOR] Variant
[COLOR=Navy]Dim[/COLOR] Ac          [COLOR=Navy]As[/COLOR] [COLOR=Navy]Long[/COLOR]
[COLOR=Navy]Dim[/COLOR] n           [COLOR=Navy]As[/COLOR] [COLOR=Navy]Long[/COLOR]
[COLOR=Navy]Dim[/COLOR] Dic         [COLOR=Navy]As[/COLOR] Object
[COLOR=Navy]Dim[/COLOR] Q           [COLOR=Navy]As[/COLOR] Variant
[COLOR=Navy]Dim[/COLOR] Txt         [COLOR=Navy]As[/COLOR] [COLOR=Navy]String[/COLOR]
[COLOR=Navy]Dim[/COLOR] k           [COLOR=Navy]As[/COLOR] Variant
[COLOR=Navy]Dim[/COLOR] p           [COLOR=Navy]As[/COLOR] Variant
[COLOR=Navy]Dim[/COLOR] c           [COLOR=Navy]As[/COLOR] [COLOR=Navy]Long[/COLOR]

Ray = ActiveSheet.Cells(1).CurrentRegion
 [COLOR=Navy]Set[/COLOR] Dic = CreateObject("Scripting.Dictionary")
    Dic.CompareMode = 1
   
   [COLOR=Navy]For[/COLOR] n = 2 To UBound(Ray, 1) [COLOR=Navy]Step[/COLOR] 2
        [COLOR=Navy]For[/COLOR] Ac = 2 To UBound(Ray, 2)
            Txt = Ray(n, Ac) & "," & Ray(n + 1, Ac)
            [COLOR=Navy]If[/COLOR] Not Dic.exists(Ray(n, 1)) [COLOR=Navy]Then[/COLOR]
                [COLOR=Navy]Set[/COLOR] Dic(Ray(n, 1)) = CreateObject("Scripting.Dictionary")
            [COLOR=Navy]End[/COLOR] If
        
        [COLOR=Navy]If[/COLOR] Not Dic(Ray(n, 1)).exists(Txt) [COLOR=Navy]Then[/COLOR]
                Dic(Ray(n, 1)).Add (Txt), Array(Ray(1, Ac), Ray(1, Ac))
        [COLOR=Navy]Else[/COLOR]
            Q = Dic(Ray(n, 1)).Item(Txt)
                Q(1) = Ray(1, Ac)
            Dic(Ray(n, 1)).Item(Txt) = Q
        [COLOR=Navy]End[/COLOR] If
    [COLOR=Navy]Next[/COLOR] Ac
    [COLOR=Navy]Next[/COLOR] n
   
   ReDim nray(1 To UBound(Ray, 1) * UBound(Ray, 2), 1 To 5)
   nray(1, 1) = "Person": nray(1, 2) = "Start": nray(1, 3) = "End"
   nray(1, 4) = "Hours": nray(1, 5) = "Rate"
   c = 1
    
    [COLOR=Navy]For[/COLOR] [COLOR=Navy]Each[/COLOR] k [COLOR=Navy]In[/COLOR] Dic.Keys
        [COLOR=Navy]For[/COLOR] [COLOR=Navy]Each[/COLOR] p [COLOR=Navy]In[/COLOR] Dic(k)
             c = c + 1
             nray(c, 1) = k
             nray(c, 2) = Dic(k).Item(p)(0)
             nray(c, 3) = Dic(k).Item(p)(1)
             nray(c, 4) = Split(p, ",")(0)
             nray(c, 5) = Split(p, ",")(1)
        [COLOR=Navy]Next[/COLOR] p
    [COLOR=Navy]Next[/COLOR] k
[COLOR=Navy]With[/COLOR] Sheets("Sheet2").Range("A1").Resize(c, 5)
    .Value = nray
    .Columns.AutoFit
    .Borders.Weight = 2
[COLOR=Navy]End[/COLOR] [COLOR=Navy]With[/COLOR]
[COLOR=Navy]End[/COLOR] [COLOR=Navy]Sub[/COLOR]
Regards Mick
 
Upvote 0
Hi Mick,
Thanks for coming back so quickly - suspect we might be on a significant time difference! Apologies for the revisit, turns out the problem was more complicated than I originally realised. I'm trying to avoid 6 months of pain creating this work around. We have inherently related data points being stored on a daily basis, but they are independent of each other. We need to be able to see what occurred over a given time period, not just on any given day. As such, the data in raw format appears as below;
Person ID
Data type
1/01/2018
2/01/2018
3/01/2018
4/01/2018
5/01/2018
6/01/2018
7/01/2018
8/01/2018
9/01/2018
10/01/2018
1111
Hours
4
4
4
4
4
4
5
5
5
5
1111
Rate
18.5
18.5
18.5
18.5
18.5
19
19
19
19
19
1111
Units of #1
8500
8500
8500
8500
8500
9000
9000
9000
9000
9000
1111
Units of #2
1200
1200
1200
1200
1200
1200
1200
900
900
900
1111
Transport (mins)
35
35
35
35
35
35
35
45
45
45
1111
Data (gb)
1.1
1.1
1.1
1.1
1.1
1.1
1.1
3
3
3
1111
Territory Code
xxyy2
xxyy2
xxyy4
xxyy4
xxyy2
xxyy2
xxyy2
xxyy2
xxyy2
xxyy2
1111
Region Code
xxe
xxe
xxe
yyr
yyr
yyr
yyr
yyr
yyr
yyr
1112
Hours
8
8
8
8
8
8
8
7
7
7
1112
Rate
21
21
21
21
21
21
21
21
21
21
1112
Units of #1
4500
4500
4500
4500
4500
4500
4500
4500
3500
3500
1112
Units of #2
1200
1200
1200
1200
1200
1200
2100
2100
2100
2100
1112
Transport (mins)
15
15
15
15
15
15
15
15
15
15
1112
Data (gb)
1.1
1.1
1.1
1.1
2.5
2.5
2.5
3
3
3
1112
Territory Code
xxyy2
xxyy2
xxyy4
xxyy4
xxyy4
xxyy4
xxyy4
xxyy5
xxyy5
xxyy5
1112
Region Code
xxq
xxq
xxq
xxq
xxq
xxq
xxq
xxq
xx4
xx4

<tbody>
</tbody>

The output should look like
Person ID
Start
End
Hours
Rate
Units of #1
Units of #2
Transport (mins)
Data (gb)
Territory Code
Region Code
1111
1/01/2018
3/01/2018
4
18.5
8500
1200
35
1.1
xxyy4
xxe
1111
4/01/2018
4/01/2018
4
18.5
8500
1200
35
1.1
xxyy4
yyr
1111
5/01/2018
5/01/2018
4
18.5
8500
1200
35
1.1
xxyy2
yyr
1111
6/01/2018
6/01/2018
4
19
9000
1200
35
1.1
xxyy2
yyr
1111
7/01/2018
7/01/2018
5
19
9000
1200
35
1.1
xxyy2
yyr
1111
8/01/2018
10/01/2018
5
19
9000
900
45
3
xxyy2
yyr

<tbody>
</tbody>

Any thoughts appreciated. Thank you again
 
Upvote 0
Sorry for late reply:-
Try this for results in sheet2.
Code:
[COLOR="Navy"]Sub[/COLOR] MG14Nov14
[COLOR="Navy"]Dim[/COLOR] Ray         [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Dim[/COLOR] Ac          [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] n           [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Dic         [COLOR="Navy"]As[/COLOR] Object
[COLOR="Navy"]Dim[/COLOR] Q           [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Dim[/COLOR] Txt         [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Dim[/COLOR] k           [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Dim[/COLOR] p           [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Dim[/COLOR] c           [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]

Ray = ActiveSheet.Cells(1).CurrentRegion
 [COLOR="Navy"]Set[/COLOR] Dic = CreateObject("Scripting.Dictionary")
    Dic.CompareMode = 1
   
   [COLOR="Navy"]For[/COLOR] n = 2 To UBound(Ray, 1) [COLOR="Navy"]Step[/COLOR] 8
        [COLOR="Navy"]For[/COLOR] Ac = 3 To UBound(Ray, 2)
           [COLOR="Navy"]With[/COLOR] Application
                Txt = Join(.Transpose(.Index(Ray, Evaluate("row(" & n & ":" & n + 7 & ")"), Ac)), ",")
            [COLOR="Navy"]End[/COLOR] With
            [COLOR="Navy"]If[/COLOR] Not Dic.exists(Ray(n, 1)) [COLOR="Navy"]Then[/COLOR]
                [COLOR="Navy"]Set[/COLOR] Dic(Ray(n, 1)) = CreateObject("Scripting.Dictionary")
            [COLOR="Navy"]End[/COLOR] If
        
        [COLOR="Navy"]If[/COLOR] Not Dic(Ray(n, 1)).exists(Txt) [COLOR="Navy"]Then[/COLOR]
                Dic(Ray(n, 1)).Add (Txt), Array(Ray(1, Ac), Ray(1, Ac))
        [COLOR="Navy"]Else[/COLOR]
            Q = Dic(Ray(n, 1)).Item(Txt)
                Q(1) = Ray(1, Ac)
            Dic(Ray(n, 1)).Item(Txt) = Q
        [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Next[/COLOR] Ac
    [COLOR="Navy"]Next[/COLOR] n
   
   ReDim nRay(1 To UBound(Ray, 1) * UBound(Ray, 2), 1 To 11)
   nRay(1, 1) = "Person": nRay(1, 2) = "Start": nRay(1, 3) = "End"
   nRay(1, 4) = "Hours": nRay(1, 5) = "Rate": nRay(1, 6) = "Units of [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] "
   nRay(1, 7) = "Units of [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=2]#2[/URL] ": nRay(1, 8) = "Transport (mins)"
   nRay(1, 9) = "Data (gb)": nRay(1, 10) = "Territory Code": nRay(1, 11) = "Region Code"
    c = 1
    
    [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] k [COLOR="Navy"]In[/COLOR] Dic.Keys
        [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] p [COLOR="Navy"]In[/COLOR] Dic(k)
             c = c + 1
             nRay(c, 1) = k
             nRay(c, 2) = Dic(k).Item(p)(0)
             nRay(c, 3) = Dic(k).Item(p)(1)
             nRay(c, 4) = Split(p, ",")(0)
             nRay(c, 5) = Split(p, ",")(1)
             nRay(c, 6) = Split(p, ",")(2)
             nRay(c, 7) = Split(p, ",")(3)
             nRay(c, 8) = Split(p, ",")(4)
             nRay(c, 9) = Split(p, ",")(5)
             nRay(c, 10) = Split(p, ",")(6)
             nRay(c, 11) = Split(p, ",")(7)
        [COLOR="Navy"]Next[/COLOR] p
    [COLOR="Navy"]Next[/COLOR] k

[COLOR="Navy"]With[/COLOR] Sheets("Sheet2").Range("A1").Resize(c, 11)
    .Value = nRay
    .Columns.AutoFit
    .Borders.Weight = 2
[COLOR="Navy"]End[/COLOR] With


[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Mick - this has worked perfectly. I'm going to try on a much larger data set, and see how that goes but not expecting errors (unless by my own making frankly). The code is very efficient on my test data.

You have saved me a lot of time!! Thank you!!!
 
Upvote 0

Forum statistics

Threads
1,214,645
Messages
6,120,711
Members
448,984
Latest member
foxpro

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