calculate counts of days for consecutive five dates based on time for item

Hasson

Active Member
Joined
Apr 8, 2021
Messages
390
Office Version
  1. 2016
Platform
  1. Windows
hi
I would calculate days for consecutive five dates based on time for item . if I have five or more consecutive five dates contain time is in column C 00.00 then should calculate days for consecutive five dates for item in column A as is highlighted in column B the result should show in columns I,J
a.xlsx
ABCDEFGHIJ
1CODEDATETIMECODEDAYS
2AA-S01/10/202113:00:00 صAA-S10 DAYS
3AA-S02/10/202100:00BB-DD15 DAYS
4AA-S05/10/202100:00MM-NN11DAYS
5AA-S10/10/202100:00
6AA-S11/10/202100:00
7AA-S12/10/202100:00
8AA-S13/10/202100:00
9AA-S14/10/202100:00
10AA-S15/10/202100:00
11BB-DD01/10/202114:00:00 ص
12BB-DD02/10/202100:00
13BB-DD15/10/202100:00
14BB-DD20/10/202100:00
15BB-DD26/10/202100:00
16BB-DD27/10/202100:00
17BB-DD28/10/202100:00
18BB-DD29/10/202100:00
19BB-DD30/10/202100:00
20SS-NN01/10/202100:00
21SS-NN02/10/202100:00
22SS-NN15/10/202100:00
23SS-NN20/10/202100:00
24SS-NN24/10/202100:00
25SS-NN25/10/202100:00
26SS-NN26/10/202100:00
27SS-NN27/10/202117:00:00 ص
28SS-NN28/10/202100:00
29MM-NN15/10/202100:00
30MM-NN20/10/202100:00
31MM-NN24/10/202100:00
32MM-NN25/10/202100:00
33MM-NN26/10/202100:00
Sheet1
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
What do you consider a "five date"? From your example, it seems to be a date where the day of the month is a multiple of 5. This holds true, except for the last date you have marked 26/10/2021. Assuming that is the case, then I think this is too complicated for a native Excel function. I wrote up a User Defined Function though. If I understand your requirements correctly, and you're OK with a User Function, then here's the function:

VBA Code:
Public Function Consec5(MyTab As Range, MyCode As String)
Dim MyDat As Variant, loc1 As Long, loc2 As Long, i As Long, w As Long, mr As Long

    MyDat = MyTab.Value
    
    loc1 = 0
    loc2 = 0
    mr = 0
    
    For i = 1 To UBound(MyDat)
        If MyDat(i, 1) = MyCode Then
            If MyDat(i, 3) = 0 Then
                If Day(MyDat(i, 2)) Mod 5 = 0 Then
                    If loc1 = 0 Then
                        loc1 = i
                    Else
                        loc2 = i
                    End If
                End If
            Else
                If loc1 > 0 And loc2 > 0 Then
                    w = MyDat(loc2, 2) - MyDat(loc1, 2)
                    If w > mr Then mr = w
                End If
                loc1 = 0
                loc2 = 0
            End If
        Else
            If loc1 > 0 And loc2 > 0 Then
                w = MyDat(loc2, 2) - MyDat(loc1, 2)
                If w > mr Then mr = w
            End If
            loc1 = 0
            loc2 = 0
        End If
    Next i
    
    If loc1 > 0 And loc2 > 0 Then
        w = MyDat(loc2, 2) - MyDat(loc1, 2)
        If w > mr Then mr = w
    End If

    If mr >= 5 Then
        Consec5 = mr
    End If
    
End Function

Call it like this:

Book1
ABCDEFGHIJ
1CODEDATETIMECODEDAYS
2AA-S10/1/202113:00:00 صAA-S10
3AA-S10/2/20210BB-DD15
4AA-S10/5/20210MM-NN10
5AA-S10/10/20210
6AA-S10/11/20210
7AA-S10/12/20210
8AA-S10/13/20210
9AA-S10/14/20210
10AA-S10/15/20210
11BB-DD10/1/202114:00:00 ص
12BB-DD10/2/20210
13BB-DD10/15/20210
14BB-DD10/20/20210
15BB-DD10/26/20210
16BB-DD10/27/20210
17BB-DD10/28/20210
18BB-DD10/29/20210
19BB-DD10/30/20210
20SS-NN10/1/20210
21SS-NN10/2/20210
22SS-NN10/15/20210
23SS-NN10/20/20210
24SS-NN10/24/20210
25SS-NN10/25/20210
26SS-NN10/26/20210
27SS-NN10/27/202117:00:00 ص
28SS-NN10/28/20210
29MM-NN10/15/20210
30MM-NN10/20/20210
31MM-NN10/24/20210
32MM-NN10/25/20210
33MM-NN10/26/20210
Sheet17
Cell Formulas
RangeFormula
J2:J4J2=consec5($A$2:$C$33,I2)


If that's not what you want, let me know and I'll take another look.
 
Upvote 0
except for the last date you have marked 26/10/2021.
I find difficulty to understand this sentence what do you mean? why MM-NN gives wrong value it should give 11 not 10 when calculate last fives dates?
when show the result should also show the item in column I (CODE)
 
Upvote 0
How do you choose which dates to include? Your first range has 7, the next has 7, the last has 5. My next guess is your start date is the first date in the range with a time of 00:00, and the last date is the last date in the range.
 
Upvote 0
My next guess is your start date is the first date in the range with a time of 00:00, and the last date is the last date in the range.
yes this is correct. so why gives wrong value?
 
Upvote 0
See if this does what you want:

Book1
ABCDEFGHIJ
1CODEDATETIMECODEDAYS
2AA-S10/1/202113:00:00 صAA-S10
3AA-S10/2/20210BB-DD15
4AA-S10/5/20210MM-NN11
5AA-S10/10/20210
6AA-S10/11/20210
7AA-S10/12/20210
8AA-S10/13/20210
9AA-S10/14/20210
10AA-S10/15/20210
11BB-DD10/1/202114:00:00 ص
12BB-DD10/2/20210
13BB-DD10/15/20210
14BB-DD10/20/20210
15BB-DD10/26/20210
16BB-DD10/27/20210
17BB-DD10/28/20210
18BB-DD10/29/20210
19BB-DD10/30/20210
20SS-NN10/1/20210
21SS-NN10/2/20210
22SS-NN10/15/20210
23SS-NN10/20/20210
24SS-NN10/24/20210
25SS-NN10/25/20210
26SS-NN10/26/20210
27SS-NN10/27/202117:00:00 ص
28SS-NN10/28/20210
29MM-NN10/15/20210
30MM-NN10/20/20210
31MM-NN10/24/20210
32MM-NN10/25/20210
33MM-NN10/26/20210
Sheet17
Cell Formulas
RangeFormula
J2:J4J2=LOOKUP(2,1/($A$2:$A$33=I2),$B$2:$B$33)-AGGREGATE(15,6,$B$2:$B$33/($A$2:$A$33=I2)/(MOD(DAY($B$2:$B$33),5)=0)/($C$2:$C$33=0),1)
 
Upvote 0
great ! waht about CODE in column I ? shouldn't writing
 
Upvote 0

Forum statistics

Threads
1,214,986
Messages
6,122,611
Members
449,090
Latest member
vivek chauhan

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