简体   繁体   English

VBA工作日函数返回的值与Excel工作日函数不同

[英]VBA Weekday function returns different value from Excel Weekday function

I'm completely stumped - 我完全迷住了-

Using the Weekday() function in vba is returning the incorrect weekday, while using it within a sheet in Excel is returning the correct weekday. 在vba中使用Weekday()函数返回不正确的工作日,而在Excel中的工作表中使用它则返回正确的工作日。 Specifying or omitting the start-of-week arguments does not seem to be making any difference either. 指定或省略星期开始的参数似乎也没有任何区别。

The system clock (which is what I assume it uses for 'today') has not been changed. 系统时钟(这是我认为它用于“今天”的时钟)尚未更改。 In fact, I don't even have access to change it. 实际上,我什至无权更改它。

This is what my immediate window in VBA returned: 这是我在VBA中的即时窗口返回的内容:

Print Weekdayname(Weekday(today),False)
Saturday

Print Weekday(today)
 7 

Print Application.WorksheetFunction.Weekday(today)
 7 

Print Weekday(6/5/2014)
 7 

Print Weekday(6/13/2014)
 7 

The excel sheet, on the other hand, returns 5 for these formulas: 另一方面,Excel工作表将为这些公式返回5:

=WEEKDAY(TODAY())
=WEEKDAY(DATEVALUE("06/5/2014"))

Any help would be greatly appreciated! 任何帮助将不胜感激!

Edit: 编辑:

I am using Excel 2010 on Windows 7. 我在Windows 7上使用Excel 2010。

The problem is "today" will not return anything. 问题是“今天”将不会返回任何内容。 Use "now": 现在使用”:

Weekdayname(Weekday(Now),False)

Also, for your last functions where you are inputting the date, you need to put it in quotes: 另外,对于您输入日期的最后一个函数,您需要将其用引号引起来:

Weekday("6/5/2014")

You try to display the weekday of a Date serial number that's result of the calculation 6/5/2014 (ie 6 divided by 5 then divided by 2014). 您尝试显示由6/5/2014计算6/5/2014的日期序列号的6/5/2014 (即6除以5再除以2014)。

If you want to use literal values of type Date, try in your Immediate window: 如果要使用日期类型的文字值,请在“立即”窗口中尝试:

?Weekday(#6/5/2014#)

Please note the difference between this and: 请注意以下内容与:

?Weekday(6/5/2014)

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

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