A neat way to get last Friday

no matter what day I call the function.

I know that I could write a selectday weekday (now) statement, it was just interesting, is there an easier way to go?

+5
source share
4 answers

Did this help you get started? I just gave it a quick test and it seemed to work fine.

Private Sub LastFriday()

    Dim iWeekday As Integer, LastFridayDate As Date

    iWeekday = Weekday(Now(), vbFriday)

    LastFridayDate = Format(Now - (iWeekday - 1), "dd-mmm-yy")

End Sub
+8
source

DateAdd ("d", -1 - Day of the week (now), now)

+4
source
DatePart('dddd', now)

DatePart('dddd', #1/1/2010#) 

... .

0

, , "oldDay = 2" .. , ,

Dim todaysDate As Date = Date.Today       Dim oldDay As Integer       Dim thisWeek As Date

    Dim firstWeek As Date
    Dim secondWeek As Date
    Dim thirdWeek As Date
    Dim fourthWeek As Date


    ''finds the Friday of the end of the current week 
    ''No mattter what day you are working 
    Dim daycount As Integer

    oldDay = Weekday(todaysDate)
    thisWeek = todaysDate

    If oldDay < 6 Then
        daycount = 6 - oldDay
        thisWeek = thisWeek.AddDays(+daycount)
    ElseIf oldDay > 6 Then
        daycount = oldDay - 6
        thisWeek = thisWeek.AddDays(-daycount)
    End If


    firstWeek = thisWeek
    secondWeek = thisWeek
    thirdWeek = thisWeek
    fourthWeek = thisWeek

    fourthWeek = firstWeek.AddDays(-28)
    thirdWeek = thirdWeek.AddDays(-21)
    secondWeek = secondWeek.AddDays(-14)
    firstWeek = firstWeek.AddDays(-7)
0

All Articles