VBA/Excel/Access/Word/Date Functions/Weekday

Материал из VB Эксперт
Перейти к: навигация, поиск

Get the weekend between startDate and endDate

Function TestIfWeekend(startDate, endDate)
  Dim varDate
  TestIfWeekend = "XXX"                  
  If startDate = 0 Then Exit Function    
  If endDate = 0 Then Exit Function      
  If endDate - startDate < 2 Then Exit Function  
  For varDate = startDate To endDate - 1
    If WeekDay(varDate) = 7 Then
      TestIfWeekend = "OK": Exit Function
    End If
  Next varDate
End Function

The Weekday function values

Constant     Value 
vbSunday     1
vbMonday     2
vbTuesday    3
vbWednesday  4
vbThursday   5
vbFriday     6
vbSaturday   7

Use Weekday to get the weekday value from a date

Private Sub myTime1()
    Dim time As Date
    Dim theHour As Integer
    Dim theDayOfTheWeek As Integer
    time = Now
    theHour = Hour(time)
    theDayOfTheWeek = Weekday(time)
    If (theHour > 8) And (theHour < 17) Then
        If (theDayOfTheWeek > 0) And (theDayOfTheWeek < 6) Then
            MsgBox ("You should be at work!")
            MsgBox ("I love weekends")
        End If
        MsgBox ("You should not be at work!")
    End If
End Sub


Sub Main2()
   Debug.Print "Today"s date is: " & Date
   Debug.Print "The day of the week is: " & Weekday(Date)
End Sub

Weekday(date) returns A Variant/Integer containing the day of the week represented by date

Sub dateDemo16()
   Debug.Print Weekday(#4/1/2006#)
End Sub