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

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
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,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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