• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 70
  • Last Modified:

Need to find last vbFriday

Need to find last vbFriday vba
0
dailymeat
Asked:
dailymeat
  • 4
  • 4
  • 2
1 Solution
 
Subodh Tiwari (Neeraj)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

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

0
 
aikimarkCommented:
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

0
 
aikimarkCommented:
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

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Subodh Tiwari (Neeraj)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

Open in new window


Sub Test()
MsgBox getLastFriday(Date)
End Sub

Open in new window

0
 
dailymeatAuthor 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
 
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
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

0
 
aikimarkCommented:
Did you test my solutions
0
 
dailymeatAuthor 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
 
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
You're welcome! Glad it worked as desired.
0
 
aikimarkCommented:
So, you really needed "Prior or current Friday", not "Last Friday"
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 4
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now