Could any body help me to find 1st working day in the month.

Eric Penfold

Active Member
Joined
Nov 19, 2021
Messages
431
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
This code finds first day in month but i need first working day i.e. 02/04 at the moment it returns 01/04.

VBA Code:
Function fwday(Optional givenDate As Variant) As Date

    Dim d As Date

    If IsMissing(givenDate) Then givenDate = Date

    d = DateSerial(Year(givenDate), Month(givenDate), 1)
    d = d + ((9 - (d Mod 7)) Mod 7)
    fwday = d
    
End Function
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
The code below seems to work. Managed to work it out myself.

VBA Code:
Option Explicit

Sub FillDates()

Dim wb As Workbook
Dim ws As Worksheet
Dim FCell As Range
Dim LCell As Range
Dim LRow   As Long

Set wb = Workbooks("DailyMail.xlsx")
Set ws = wb.Worksheets("Daily Mail Update")
Set FCell = ws.Range("A2")
Set LCell = ws.Range("A2" & LRow)
LRow = ws.Cells(Rows.Count, 1).End(xlUp).Row

If Not Date = Application.WorkDay(DateSerial(Year(Date), Month(Date), 0), 1) Or _
    Date = Application.WorkDay(DateSerial(Year(Date), Month(Date), 0), 2) Then
    
FCell.Clear

FCell = Evaluate("Workday(EOMonth(Now(),-1),1)")
   
With FCell
    .HorizontalAlignment = xlCenter
    .NumberFormat = ("dd/mm/yy")
End With

End If

End Sub
 
Upvote 0
Here's a shorter version

VBA Code:
Public Function fwday(Optional givenDate As Variant) As Date
    Dim dtTemp As Date, wdTemp As Integer
    If IsMissing(givenDate) Then givenDate = Date
    dtTemp = DateSerial(Year(givenDate), Month(givenDate), 1)
    wdTemp = Weekday(dtTemp)
    If wdTemp = 1 Then dtTemp = DateSerial(Year(givenDate), Month(givenDate), 2)
    If wdTemp = 7 Then dtTemp = DateSerial(Year(givenDate), Month(givenDate), 3)
    fwday = dtTemp
End Function
 
Upvote 0
Solution
Here's a shorter version

VBA Code:
Public Function fwday(Optional givenDate As Variant) As Date
    Dim dtTemp As Date, wdTemp As Integer
    If IsMissing(givenDate) Then givenDate = Date
    dtTemp = DateSerial(Year(givenDate), Month(givenDate), 1)
    wdTemp = Weekday(dtTemp)
    If wdTemp = 1 Then dtTemp = DateSerial(Year(givenDate), Month(givenDate), 2)
    If wdTemp = 7 Then dtTemp = DateSerial(Year(givenDate), Month(givenDate), 3)
    fwday = dtTemp
End Function
Thanks much neater I will use this.
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,191
Members
452,616
Latest member
intern444

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