Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 07-07-2025, 07:51 PM
Chris G Chris G is offline Sub routine for generating second week number of current fortnight pay period Windows 11 Sub routine for generating second week number of current fortnight pay period Office 2016
Novice
Sub routine for generating second week number of current fortnight pay period
 
Join Date: Oct 2023
Posts: 9
Chris G is on a distinguished road
Default Sub routine for generating second week number of current fortnight pay period

Is this the best way to generate in a future proof way the week number, representing whatever is the SECOND week of the current (fortnightly) pay period?



This code below is on a command button click event on a sheet in a timesheet template, and having tried a few different options, this gives the correct result today, July 8 2025, of 28. A lot of dates get updated from this value, and while cell U4 also has a drop down list, it would be nice that it always worked from a command button.

Code:
    Dim referenceStart As Date
    Dim currentDate As Date
    Dim daysSinceReference As Long
    Dim fortnightIndex As Long
    Dim fortnightStart As Date
    Dim secondWeekStart As Date
    Dim isoWeek As Integer

    ' Reference start date (must be a known Monday that starts a fortnight)
    referenceStart = DateSerial(2025, 7, 1) ' Tuesday 1 July 2025 — adjust to Monday
    If Weekday(referenceStart, vbMonday) <> 1 Then
        referenceStart = referenceStart - Weekday(referenceStart, vbMonday) + 1 ' Adjust to nearest previous Monday
    End If

    currentDate = Date
    daysSinceReference = currentDate - referenceStart
    fortnightIndex = Int(daysSinceReference / 14)
    fortnightStart = referenceStart + (fortnightIndex * 14)
    secondWeekStart = fortnightStart + 7

    ' Get ISO week number
    isoWeek = Application.WorksheetFunction.IsoWeekNum(secondWeekStart)

    ' Output to cell U4
    Worksheets("Fortnightly timesheet").Range("U4").Value = isoWeek
Reply With Quote
 

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Generating reports that shows an individuals planning per week Zweebo Project 1 02-18-2023 10:52 PM
Generating random number of integers in a specific manner HussainAlqatari Excel 2 10-16-2022 03:47 PM
Sub routine for generating second week number of current fortnight pay period Find number and words without a period and bold jeffreybrown Word 4 08-20-2019 05:58 PM
Sub routine for generating second week number of current fortnight pay period generating a number in Word John P Word 1 05-31-2012 10:29 PM
Sub routine for generating second week number of current fortnight pay period setting the number of spaced after a period. -- need a new dictionary as well Straitsfan Word 1 10-31-2011 06:20 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:54 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft