简体   繁体   English

如何在VBA语句中使用WEEKNUM函数?

[英]How can I use the WEEKNUM function in a statement in VBA?

I want to write a VBA, to check if WEEKNUM or ISOWEEKNUM equal to the value, then run the rest of macro. 我想写一个VBA,检查WEEKNUM或ISOWEEKNUM是否等于该值,然后运行其余的宏。 I've tried to do that but I got an error because of using TODAY as arg. 我试过这样做但是因为使用TODAY作为arg我得到了一个错误。

Use the WorksheetFunction object or Excel Application object to call native functions into VBA. 使用WorksheetFunction对象Excel Application对象将本机函数调用到VBA中。

debug.print WorksheetFunction.WeekNum(Date)
debug.print application.WeekNum(Date)
debug.print WorksheetFunction.IsoWeekNum(Date)
debug.print application.IsoWeekNum(Date)
if application.WeekNum(Date) = 28 then
     'it is currently 28
end if

In VBA, the TODAY() function is replaced by Date . 在VBA中, TODAY()函数由Date替换。

The WEEKNUM function considers the week containing January 1 to be the first week of the year. WEEKNUM函数将包含1月1日的一周视为一年中的第一周。 However, there is a European standard that defines the first week as the one with the majority of days (four or more) falling in the new year. 但是,有一个欧洲标准将第一周定义为在新的一年中大部分时间(四个或更多)下降的那个星期。 This means that for years in which there are three days or less in the first week of January, the WEEKNUM function returns week numbers that are incorrect according to the European standard. 这意味着,在1月的第一周中有三天或更少的年份,WEEKNUM函数返回根据欧洲标准不正确的周数。

Sub test()
    Dim wn As Integer
    wn = Format(Date, "ww")
    Debug.Print wn
End Sub

ISO Weeknum UDF is. ISO Weeknum UDF是。
The ISO weeknumber ISO周数

The ISO-criteria for the first week in a year: - a week starts at monday; 一年中第一周的ISO标准: - 一周从星期一开始; so monday is the first day of any week - the first week in a year must contain at least 4 days in that year 所以星期一是任何一周的第一天 - 一年中的第一周必须包含该年至少4天

  • the 1st of january is in week 1 if it's a monday, tuesday, wednesday or thursday 如果是星期一,星期二,星期三或星期四,1月1日是在第1周
  • the 4th of january is always in week 1 of any year 1月4日总是在任何一年的第1周
  • the first thursday in a year is always in week 1 一年中的第一个星期四总是在第1周

     Public Function ISOweeknum(ByVal v_Date As Date) As Integer ISOweeknum = DatePart("ww", v_Date - Weekday(v_Date, 2) + 4, 2, 2) End Function 

    To test this Function another small routine can be added. 要测试此功能,可以添加另一个小例程。

      Sub test_today_weeknum() Dim dt As Date Dim wno As Integer 'In this example, the variable called LDate would now contain the current system date. dt = Date Debug.Print dt wno = ISOweeknum(dt) Debug.Print wno End Sub 

For today ie 9-7-2016, First one gives weeknum 28 whereas ISO weeknum function gives 27 对于今天即9-7-2016,第一个给出第28周,而ISO周期函数给出27

For 9-7-2015 both the above routines will give weeknum 28 对于9-7-2015,上述例程都将给出第28周

To get the current day (without the time portion) in a cell formula in Excel, use: 要在Excel中的单元格公式中获取当前日期(没有时间部分),请使用:

=TODAY()

In the VBA programming environment, however, the function would be DATE() . 但是,在VBA编程环境中,函数将是DATE() Now contains both the current date and time while Date and Time are seperate commands for the date and time.It all depends on which information you need. Now包含当前日期和时间,而DateTimeDateTime的单独命令。这取决于您需要哪些信息。

Here is one way to use both WEEKNUM() and TODAY() in VBA: 这是在VBA中同时使用WEEKNUM()TODAY()一种方法:

Sub dural()
    If Evaluate("=weeknum(today())") = 28 Then
        MsgBox 28
    Else
        MsgBox "not 28"
    End If
End Sub

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

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