Consecutive Weeks

errollflynn

New Member
Joined
Jan 25, 2021
Messages
24
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have a payroll report that has a list of pay periods and associated pay dates where a single employee will have multiple rows for multiple dates. I am trying to find a way to identify instances of consecutive dates with the goal being to find instances where an employee has received payment over the course of 10 consecutive pay periods.

Thank you.
 

Attachments

  • Sample Data.JPG
    Sample Data.JPG
    107.3 KB · Views: 15

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Here is a UDF (userdefinedfunction) to do the counting for you. Put it in a VBA module and save your workbook as a .xlsm type.

VBA Code:
Function ConsecutivePayDays(sKey As String, rPayTable As Range, rKey As Range, rPayPeriod As Range, Optional bDescendingDates As Boolean = True) As Long
    Dim vIn As Variant
    Dim lR As Long, lCkey As Long, lCpayp As Long, lCount As Long, lUB1 As Long, lPayInt As Long
    Dim dtLast As Date
    Const lPAYINTERVAL As Long = 7      '<<<<<<< payment interval = each 7 days
    
    If Intersect(rPayTable, rKey) Is Nothing Or Intersect(rPayTable, rPayPeriod) Is Nothing Then
        ConsecutivePayDays = -1
        Exit Function
    End If
    
    vIn = rPayTable.Value
    lUB1 = UBound(vIn, 1)
    
    lCkey = rKey.Column - rPayTable.Column + 1
    lCpayp = rPayPeriod.Column - rPayTable.Column + 1
    
    lPayInt = IIf(bDescendingDates, -lPAYINTERVAL, lPAYINTERVAL)
    For lR = 1 To lUB1
        If vIn(lR, lCkey) Like sKey Then
            Select Case True
                Case lCount = 0
                    'first encounter of Key
                    lCount = 1
                    dtLast = vIn(lR, lCpayp)
                Case vIn(lR, lCpayp) = dtLast + lPayInt
                    'date is consecutive with previous
                    lCount = lCount + 1
                    dtLast = vIn(lR, lCpayp)
                Case vIn(lR, lCpayp) = vIn(lR - 1, lCpayp)
                    'Same paydate as line above - do nothing
                Case Abs(vIn(lR, lCpayp) - vIn(lR - 1, lCpayp)) > lPAYINTERVAL
                    'not consecutive - stop counting
                    Exit For
            End Select
        End If
    Next lR
    ConsecutivePayDays = lCount
End Function


To use it start typing
=cons
and you will see the ConsecutivePayDays() function in the proposed list. double click it.
There are 5 parameters. The 5th you can leave out if your table is sorted with descending dates

=ConsecutivePayDays(Keyname or ID to look for, DataTable, Column in table with the Key or ID, Column in table with PayPeriod, Descending (true= default) or ascending( false))
 
Upvote 0
jun23.xlsm
ABCDEFGHIJK
1KeyLNFNFullNSSNHrsPPsPpeLPD
2lname124/04/2022lname211
3lname117/04/2022lname15
4lname110/04/2022
5lname103/04/2022
6lname127/03/2022
7lname213/11/2022
8lname206/11/2022
9lname230/10/2022
10lname223/10/2022
11lname223/10/2022
12lname216/10/2022
13lname209/10/2022
14lname202/10/2022
15lname225/09/2022
16lname218/09/2022
17lname211/09/2022
18lname211/09/2022
19lname204/09/2022
20lname220/09/2020
21lname213/09/2020
22lname206/09/2020
23lname230/08/2020
Sheet6
Cell Formulas
RangeFormula
K2K2=ConsecutivePayDays(J2,A1:I23,A1:A23,G1:G23)
K3K3=ConsecutivePayDays(J3,A1:I23,A1:A23,G1:G23)
G3:G6,G8:G10,G12:G17,G19,G21:G23G3=G2-7
G11,G18G11=G10
G20G20=G19-714
 
Upvote 0

Forum statistics

Threads
1,215,429
Messages
6,124,842
Members
449,193
Latest member
MikeVol

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