简体   繁体   English

Excel VBA-如果文件创建日期的星期几与单元格值匹配?

[英]Excel VBA - if weekday of file created date matches cell value?

I have a folder directory on a local server at work: 我在工作的本地服务器上有一个文件夹目录:

G:\\Allocations G:\\分配

1.PDF - Created 16/12/16

I am also using the following VBA if statement within excel to display a message if the weekday name of a file's created date matches my value in cell A1. 如果文件的创建日期的工作日名称与我在单元格A1中的值匹配,我也在Excel中使用以下VBA if语句显示一条消息。

Cell A1 = Friday 单元格A1 =星期五

If WeekdayName(Weekday(objFile.DateCreated)) = Range("A1").Value Then

MsgBox "Date Match"

Else

Msgbox "No match"

End If

For some reason if i try this code at home using my Mac, the code works fine and the date shows as a match. 由于某种原因,如果我在Mac上在家尝试使用此代码,该代码可以正常工作,并且日期显示为匹配项。

However, for some reason, when i use this code on my work computer, linked to the server, i have to replace Friday with Saturday in order to get a match. 但是,由于某些原因,当我在连接到服务器的工作计算机上使用此代码时,必须将“星期五”替换为“星期六”才能进行匹配。

I am not sure why this is, since the file created date remains the same. 我不确定为什么会这样,因为文件创建日期保持不变。 Am i doing something wrong, and if so can someone please show me how to fix this? 我做错什么了吗?如果可以,请告诉我如何解决这个问题?

Both functions Weekday Function as well as WeekdayName Function are able to specify the first day of the week. 这两个函数Weekday Function以及WeekdayName Function都可以指定一周的第一天。 But while Weekday defaults to Sunday, WeekdayName defaults to first day of week specified in system settings. 但是,虽然“ Weekday默认为周日,但“ Weekday WeekdayName默认为系统设置中指定的一周的第一天。 Thats annoying because it must not match in every cases. 那很烦人,因为它不一定在每种情况下都匹配。

So the best practice will be to set the same property for first day of week for both: 因此,最佳做法是在两个星期的第一天都设置相同的属性:

WeekdayName(Weekday(objFile.DateCreated, vbMonday), , vbMonday)

or 要么

WeekdayName(Weekday(objFile.DateCreated, vbUseSystemDayOfWeek), , vbUseSystemDayOfWeek)

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

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