获得上周五约会的整洁方式

时间:2010-01-19 15:50:06

标签: vba

无论我什么时候打电话给这个功能。

我知道我可以写一个精选案例工作日(现在)声明,只是想知道是否有更简洁的方法去?

5 个答案:

答案 0 :(得分:11)

这有助于您入门吗?我刚给它一个快速测试,似乎工作正常。

Private Sub LastFriday()

    Dim iWeekday As Integer, LastFridayDate As Date

    iWeekday = Weekday(Now(), vbFriday)

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

End Sub

答案 1 :(得分:4)

DateAdd(“d”, - 1 - 工作日(现在),现在)

答案 2 :(得分:0)

DatePart('dddd', now)

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

......明确约会。

答案 3 :(得分:0)

此功能将在上个月的每个星期五找到,您可以将其更改为适合其他日期,例如星期一和#34; oldDay = 2"等方法将从今天开始工作,但你可以改变它以适应

Dim todaysDate As Date = Date.Today         Dim oldDay As Integer         把这个周日视为日期

    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)

答案 4 :(得分:0)

您可以使用这种简单(但很懒)的方法:

假设您需要获取上一个星期三,请从今天开始直到星期三继续减去1。

    Dim lastWednesday As DateTime = DateTime.Now.AddDays(-1)
    While (lastWednesday.DayOfWeek <> DayOfWeek.Wednesday)
        lastWednesday = lastWednesday.AddDays(-1)
    End While
    MsgBox(lastWednesday )

*这是VB.NET,您可以在VBA中实现相同的