简体   繁体   English

每周星期一的获取日期

[英]Getting Date of Monday of each week

I want to get the date of Monday for each week. 我想获取每周的星期一日期。 I am using that date to open a folder that is created every Monday and is named based on the date. 我正在使用该日期打开每个星期一创建的文件夹,该文件夹基于该日期命名。 I tried using weekday() function. 我尝试使用weekday()函数。 But I am not sure how to proceed. 但是我不确定如何进行。 Thanks for the help! 谢谢您的帮助! This is what i have tried, it works. 这是我尝试过的,它有效。 But I juts wanted to know if there is more efficient way to do it? 但是我想知道是否有更有效的方法?

Sub test()

Dim myday As Integer
Dim mydate As Date
mydate = Date
myday = Weekday(Date, vbMonday)

If myday = 1 Then

mydate = Date

ElseIf myday = 2 Then
mydate = DateAdd("d", -1, Date)

ElseIf myday = 3 Then
mydate = DateAdd("d", -2, Date)

ElseIf myday = 4 Then
mydate = DateAdd("d", -3, Date)

ElseIf myday = 5 Then
mydate = DateAdd("d", -4, Date)

End If

End Sub

If DT is any date, and if you want to return the same date if DT is a Monday, then, using VBA: 如果DT是任何日期,并且如果您想返回相同的日期(如果DT是星期一),则使用VBA:

Function FirstMonday(DT As Date) As Date
    FirstMonday = DT - Weekday(DT, vbMonday) + 1
End Function

Or, on your worksheet, with the Date in A1: 或者,在您的工作表上,日期在A1中:

=A1+1-WEEKDAY(A1-1)

In A1 enter: A1中输入:

1/6/2014 2014年1月6日

In A2 enter: A2中输入:

=A1 + 7

and copy down 然后抄下来

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM