vba 如何确定日期是否在周末?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1580432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:03:41  来源:igfitidea点击:

How to determine if a date falls on the weekend?

excelvbaexcel-vba

提问by JR.

Given a date as input, how can I determine whether the day falls on a weekend?

给定一个日期作为输入,我如何确定这一天是否在周末?

回答by Lawrence P. Kelley

There is a Weekday function that takes a Date as an argument and returns the day (1, 2, 3, etc.)

有一个 Weekday 函数,它将 Date 作为参数并返回日期(1、2、3 等)

The return values are:

返回值是:

vbSunday (1)  
vbMonday (2)  
vbTuesday (3)  
vbWednesday (4)  
vbThursday (5)  
vbFriday (6)  
vbSaturday (7)  

Assuming that weekends are Saturday and Sunday, the function would look like this:

假设周末是周六和周日,该函数将如下所示:

Public Function IsWeekend(InputDate As Date) As Boolean
    Select Case Weekday(InputDate)
        Case vbSaturday, vbSunday
            IsWeekend = True
        Case Else
            IsWeekend = False
    End Select
End Function

回答by Excel Hero

This is the most direct way to determine if MyDatefalls on the weekend:

这是判断是否MyDate落在周末的最直接方法:

MsgBox Weekday(MyDate, vbMonday) > 5

The Weekday() function has an optional 2nd parameter that tells it which day of the week the week starts on. The Weekday() function returns an integer from 1 through 7.

Weekday() 函数有一个可选的第二个参数,告诉它一周的哪一天开始。Weekday() 函数返回一个从 1 到 7 的整数。

I've instructed it to start the week on MONDAY and so SATURDAY would be 6 and SUNDAY would be 7.

我已经指示它从星期一开始一周,所以星期六是 6,星期日是 7。

回答by Excel Hero

Or, you can use this:

或者,你可以使用这个:

OR(MOD(WEEKDAY(cell), 7)=0, MOD(WEEKDAY(cell), 7)=1)

or

或者

MOD(WEEKDAY(cell), 7) < 2

as a formula.

作为公式。

since 1 is Sunday and 0 is Saturday

因为 1 是星期日,0 是星期六

回答by sakthi

Formula for converting date to week days

将日期转换为工作日的公式

Selection.FormulaR1C1 = "=TEXT(WEEKDAY(R7C" + CStr(i) + ",1),""ddd"")"

Selection.FormulaR1C1 = "=TEXT(WEEKDAY(R7C" + CStr(i) + ",1),""ddd"")"

this will return day

这将返回一天

eg:- if input is 02-may-2013 then it will return Thu --> (thursday) here variable i represents another column

例如:- 如果输入是 02-may-2013 那么它将返回 Thu -->(星期四)这里的变量 i 代表另一列