# Need to find last vbFriday

Need to find last vbFriday vba
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Excel & VBA ExpertCommented:
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
``````

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
``````
0
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
``````
0
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
``````
0
Excel & VBA ExpertCommented:
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
``````

``````Sub Test()
MsgBox getLastFriday(Date)
End Sub
``````
0
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.
0
Excel & VBA ExpertCommented:
You're welcome!

``````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
``````
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
Did you test my solutions
0
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.
0
Excel & VBA ExpertCommented:
You're welcome! Glad it worked as desired.
0
Commented:
So, you really needed "Prior or current Friday", not "Last Friday"
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.