Home » excel » excel – Functions to return week first date and week last date in current month

excel – Functions to return week first date and week last date in current month

Posted by: admin May 14, 2020 Leave a comment

Questions:

I created a function to get first date of week as Thursday in MS Access form and reports, but it function returns first day of week in last month.

I need it to return only current month dates. If current week first day is in last month then return first day of week as first date of week in current month.

Current output

When 1-10-2018 is selected from date picker it returns 27-09-2018, which is first date of week in last month but I need it will return first of month which is selected in textbox.

Function GetFirstofWeek(dtDate As Date)
    GetFirstofWeek = DateAdd("d", dtDate, -(Weekday(dtDate)) + 1)
         ' Excel formula for this function which is perfect and may be converted to function as required ‘=MAX(B2-WEEKDAY(B2,14)+1, EOMONTH(B2, -1)+1)
    GetFirstofWeek = DateAdd("d", dtDate, -(Weekday(dtDate, vbThursday) - 1)) 
End Function

For last day of week

Function GetLastofWeek(dtDate) as date
    '=MIN(B2+(7-WEEKDAY(B2,14)), EOMONTH(B2, 0))
     ‘this excel formula return date as I need. 
      GetLastofWeek = DateAdd("d", dtDate, (7 - (Weekday(dtDate, vbWednesday)) + 1)) 

End Function

In this function last date of week must be in current month, if month end on first date of then week will be closed and last date of week will be first date of week or days remaining in current week.

How to&Answers:

Unless I’ve misunderstood your requirements, consider the following two functions:

First of Week

Function FirstOfWeek(datRef As Date) As Date
    If Day(datRef) >= Weekday(datRef, vbThursday) Then
        FirstOfWeek = DateAdd("d", 1 - Weekday(datRef, vbThursday), datRef)
    Else
        FirstOfWeek = DateAdd("d", 1 - Day(datRef), datRef)
    End If
End Function

Some examples:

?FirstOfWeek(#2018-10-04#)
04/10/2018 

?FirstOfWeek(#2018-10-10#)
04/10/2018 

?FirstOfWeek(#2018-10-11#)
11/10/2018 

?FirstOfWeek(#2018-10-03#)
01/10/2018 

Here, the function tests whether the number of days to be subtracted in order to obtain the first day of your Thursday->Wednesday week would be less than the number of days into the current month, and if so, returns the first of the current month.

The expression DateAdd("d", 1 - Day(datRef), datRef) could alternatively be written DateSerial(Year(datRef), Month(datRef), 1) to obtain the same result.


Last of Week

Function LastOfWeek(datRef As Date) As Date
    LastOfWeek = DateAdd("d", 7 - Weekday(datRef, vbThursday), datRef)
    If Day(datRef) > Day(LastOfWeek) Then
        LastOfWeek = DateSerial(Year(datRef), Month(datRef) + 1, 0)
    End If
End Function

Some examples:

?LastOfWeek(#2018-10-04#)
10/10/2018 

?LastOfWeek(#2018-10-03#)
03/10/2018 

?LastOfWeek(#2018-09-27#)
30/09/2018 

Similar to the first function, this function first calculates the date of the following Wednesday and then checks whether the day number of this date is less than that of the supplied date (indicating that it has strayed into the following month). If so, the last day in the current month is returned.

Answer:

You need to check to see if the output month matches the input month. To avoid continuously assigning a value to GetFirstofWeek, I created a Temp date for your initial calculation. Assign your output once the validation is complete.

To call an Excel function from Access, create an instance of Excel.Application as explained by @ComputerVersteher (Thank you)

Note: Do not use this from excel due to the presence of the created object. Code is intended to be ran from Access.


Option Explicit

Function GetFirstofWeek(dtDate As Date)

Dim Temp As Date

'Temp = DateAdd("d", dtDate, -(Weekday(dtDate)) + 1)
Temp = DateAdd("d", dtDate, -(Weekday(dtDate, vbThursday) - 1))

If Month(Temp) = Month(dtDate) Then
    GetFirstofWeek = Temp
Else
    Dim obj as Object: Set obj = CreateObject("Excel.Application")
    GetFirstofWeek = obj.WorksheetFunction.EoMonth(Temp, 0) + 1
    Set obj = Nothing
End If

End Function

Function GetLastofWeek(dtDate) As Date

Dim Temp As Date

Temp = DateAdd("d", dtDate, (7 - (Weekday(dtDate, vbWednesday)) + 1))

If Month(Temp) = Month(dtDate) Then
    GetLastofWeek = Temp
Else
    Dim obj as Object: Set obj = CreateObject("Excel.Application")
    GetLastofWeek = obj.WorksheetFunction.EoMonth(Temp, 0)
    Set obj = Nothing
End If

End Function