使用WeekdayName函数获取上周一

时间:2017-05-06 10:58:40

标签: vba excel-vba excel

我想抓住上周一的日期。所以,如果今天是6/5/2017,那么如果我打开excel,我需要使用VBA返回1/5/2017(上周一)。

这是我的代码:

Dim strdate As String
Select Case WeekdayName(Now())
Case vbMonday
  strdate = Now()
Case vbTuesday
  strdate = Now() - 1
Case vbWednesday
  strdate = Now() - 2
Case vbThursday
  strdate = Now() - 3
Case vbFriday
  strdate = Now() - 4
Case vbSaturday
  strdate = Now() - 5
Case vbSunday
  strdate = Now() - 6
End Select

有人可以建议为什么这不会返回预期的结果吗?

1 个答案:

答案 0 :(得分:0)

我用这个:

Dim d As Date
    d = Date

Do Until Weekday(d) = vbMonday
    d = DateAdd("y", -1, d)
Loop

'd = date of previous Monday