Count Consecutive Dates

DeaninGA

Board Regular
Joined
May 20, 2013
Messages
146
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
So I have a list of products that may show up on a report for consecutive dates. I'd like to count the current consecutive Workdays (Monday -Friday) the product has been on the list and reset the counter once it disappears for 1 day. Thank you in advance. Dean

AB
1Report DayProduct #
25/1/2019115112
35/1/2019110123
45/1/2019995000
55/2/2019115112
65/2/201999500
75/3/2019115112
85/3/2019110123
95/6/2019995000
105/6/2019110123
115/6/2019995000
125/7/2019995000
135/8/2019115112
145/8/2019110123
155/8/2019995000
165/9/2019115112
175/9/2019110123
185/9/2019995000
195/10/2019115112

<colgroup><col style="width: 25pxpx"><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet1
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Assuming your data start "A2" then try this for results "Count", starting in column "C" against the first of each "Product #".
Code:
[COLOR="Navy"]Sub[/COLOR] MG22May12
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Q [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Set[/COLOR] Rng = Range("B2", Range("B" & Rows.Count).End(xlUp))
[COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    [COLOR="Navy"]If[/COLOR] Not .Exists(Dn.Value) [COLOR="Navy"]Then[/COLOR]
        .Add Dn.Value, Dn
    [COLOR="Navy"]Else[/COLOR]
        [COLOR="Navy"]Set[/COLOR] .Item(Dn.Value) = Union(.Item(Dn.Value), Dn)
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR]
[COLOR="Navy"]Dim[/COLOR] K [COLOR="Navy"]As[/COLOR] Variant, P [COLOR="Navy"]As[/COLOR] Range, R [COLOR="Navy"]As[/COLOR] Range, Ac [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] K [COLOR="Navy"]In[/COLOR] .keys
   Ac = 1
    [COLOR="Navy"]Set[/COLOR] R = .Item(K)(1)
    R.Offset(, Ac) = 1
        [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] P [COLOR="Navy"]In[/COLOR] .Item(K)
            [COLOR="Navy"]If[/COLOR] P.Offset(, -1) = R.Offset(, -1) + 1 [COLOR="Navy"]Then[/COLOR]
                .Item(K)(1).Offset(, Ac) = .Item(K)(1).Offset(, Ac) + 1
                [COLOR="Navy"]Set[/COLOR] R = P
            [COLOR="Navy"]ElseIf[/COLOR] P.Offset(, -1) > R.Offset(, -1) + 1 [COLOR="Navy"]Then[/COLOR]
                 Ac = Ac + 1
                 .Item(K)(1).Offset(, Ac) = 1
                    [COLOR="Navy"]Set[/COLOR] R = P
            [COLOR="Navy"]End[/COLOR] If
       [COLOR="Navy"]Next[/COLOR] P
 [COLOR="Navy"]Next[/COLOR] K
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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