简体   繁体   English

VBA将特定日期设置为一年的第一周

[英]VBA Set a specific date to be the first week of the year

I want to set a specific date (October 06 , 2014) to be the 1st Week of the year. 我想将一个特定的日期(October 06 , 2014)设置为一年的第一周。 So when I use the =weeknum() formulaR1C1 it will return the week number. 因此,当我使用=weeknum() formulaR1C1 ,它将返回星期数。

I'm currently working on a worksheet that gets updated daily so the week number will be updated every week only. 我目前正在处理一个工作表,该工作表每天更新一次,因此周数仅每周更新一次。 In column ("D") indicates the week number. 列(“ D”)中的星期几。 and column ("B") indicates the daily date. 栏(“ B”)表示每日日期。

If i use the =weeknum() formula on it returns the value of 41, but it needs to be week number 1. 如果我在其上使用=weeknum()公式,则返回值41,但它必须是第1周。

How about creating your personalized function? 如何创建个性化功能? It just consists of counting how many days there are between the date you insert (myDate) and the date which is considered to be the first day of the year (first_day), dividing this number by 7, taking its integer and adding up 1. It will always give you the right one. 它仅包括计算插入日期(myDate)和被认为是一年中第一天(first_day)的日期之间的天数,然后将该数字除以7,取整数并加1。它将始终为您提供正确的解决方案。

Public Function special_weeknum(ByVal myDate As Date) As Integer

    Dim first_day As Date: first_day = #10/6/2014#
    Dim myWeek As Integer: myWeek = Int((myDate - first_day) / 7) + 1
    special_weeknum = myWeek

End Function

Please note that this approach would allow you also to pass the first day as a user input: 请注意,这种方法还可以让您通过第一天作为用户输入:

Public Function special_weeknum(ByVal myDate As Date, ByVal first_day As Date)
    Dim myWeek As Integer: myWeek = Int((myDate - first_day) / 7) + 1
    special_weeknum = myWeek
End Function

so that you will always be able to decide which is the day you must consider as first day of the year. 这样您就可以始终决定哪一天是一年中的第一天。

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

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