Need to find last vbFriday

dailymeat
dailymeat used Ask the Experts™
on
Need to find last vbFriday vba
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Subodh Tiwari (Neeraj)Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015

Commented:
Maybe something like this...

Function getLastFriday(dt As Date) As Date
Dim wkDay As Integer
wkDay = Weekday(dt, vbFriday)
getLastFriday = dt - (wkDay - 1)
End Function

Open in new window


Then you can call this function like below...

Sub Test()
MsgBox getLastFriday(DateValue("3/14/2018")) 'Last Friday before 3/14/2018
MsgBox getLastFriday(Date)      'Last Friday from today
End Sub

Open in new window

Top Expert 2014

Commented:
If you mean the last Friday of the month, then this is a solution:
Public Function GetLastFriday(ByVal parmAnyDate As Date) As Date
    'returns the date of the last Friday in the same month as parmAnyDate
    Dim dtLastDOM As Date
    
    'Initialize to last day of the month
    dtLastDOM = DateSerial(Year(parmAnyDate), Month(parmAnyDate) + 1, 0)
    'Decrement until Friday
    Do Until Weekday(dtLastDOM) = vbFriday
        dtLastDOM = dtLastDOM - 1
    Loop
    GetLastFriday = dtLastDOM
End Function

Open in new window

Top Expert 2014

Commented:
Here's a no-looping version:
Public Function GetLastFridayNoLoop(ByVal parmAnyDate As Date) As Date
    'returns the date of the last Friday in the same month as parmAnyDate
    Dim dtLastDOM As Date
    Dim lngWeekdayDiff As Long
    
    'Initialize to last day of the month
    dtLastDOM = DateSerial(Year(parmAnyDate), Month(parmAnyDate) + 1, 0)
    'calculate days difference to Friday
    lngWeekdayDiff = Weekday(dtLastDOM) - vbFriday
    
    If Sgn(lngWeekdayDiff) = -1 Then    'adjust if negative
        dtLastDOM = dtLastDOM - lngWeekdayDiff - 7
    Else
        dtLastDOM = dtLastDOM - lngWeekdayDiff
    End If
    GetLastFridayNoLoop = dtLastDOM
End Function

Open in new window

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Subodh Tiwari (Neeraj)Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015

Commented:
If you want to find the last Friday of the month, you may also try this...

Function getLastFriday(dt As Date) As Date
getLastFriday = DateSerial(Year(dt), Month(dt) + 1, 1) - Weekday(DateSerial(Year(dt), Month(dt) + 1, 1) - 6)
End Function

Open in new window


Sub Test()
MsgBox getLastFriday(Date)
End Sub

Open in new window

Author

Commented:
Thank you.

Subodh, the function works in every day except on Fridays. If today's date is Friday, it does not show last Friday it shows today's.
Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015
Commented:
You're welcome!

Please try this...

Function getLastFriday(dt As Date) As Date
Dim wkDay As Integer
If Weekday(dt) = 6 Then dt = dt - 1
wkDay = Weekday(dt, vbFriday)
getLastFriday = dt - (wkDay - 1)
End Function

Open in new window

Top Expert 2014

Commented:
Did you test my solutions

Author

Commented:
Aikimark, I did not because your solution is for the last Friday of the month, thanks anyways for your effort.

Subodh, It worked!, thanks.
Subodh Tiwari (Neeraj)Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015

Commented:
You're welcome! Glad it worked as desired.
Top Expert 2014

Commented:
So, you really needed "Prior or current Friday", not "Last Friday"

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial