access vba获取当前日期并输出星期数
时间: 2024-09-29 12:09:21 浏览: 73
在VBA(Visual Basic for Applications)中,你可以使用`Date()`函数获取当前日期,然后结合`Weekday()`函数来获取该日期是一周中的哪一天。下面是一个简单的例子:
```vba
Sub DisplayCurrentDayOfWeek()
Dim currentDate As Date
currentDate = Date ' 获取当前日期
Dim dayOfWeek As Integer
dayOfWeek = Weekday(currentDate, vbMonday) ' 星期一为1,星期日为7
Debug.Print "今天是:" & CStr(dayOfWeek) & "号," & Weekdays(dayOfWeek) ' 输出星期几,比如"今天是:5号,星期五"
End Sub
Function Weekdays(nweekday As Long) As String ' 定义一个返回英文星期几的函数
Select Case nweekday
Case vbSunday: Weekdays = "Sunday"
Case vbMonday: Weekdays = "Monday"
Case vbTuesday: Weekdays = "Tuesday"
Case vbWednesday: Weekdays = "Wednesday"
Case vbThursday: Weekdays = "Thursday"
Case vbFriday: Weekdays = "Friday"
Case vbSaturday: Weekdays = "Saturday"
End Select
End Function
```
这个程序会显示类似这样的结果:“今天是:5号,星期五”。运行`DisplayCurrentDayOfWeek`宏即可看到当前日期及对应的星期。
阅读全文