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

拈花ヽ惹草 提交于 2020-06-27 13:10:11

问题


I want to write a VBA, to check if WEEKNUM or ISOWEEKNUM equal to the value, then run the rest of macro. I've tried to do that but I got an error because of using TODAY as arg.


回答1:


Here is one way to use both WEEKNUM() and TODAY() in VBA:

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



回答2:


Use the WorksheetFunction object or Excel Application object to call native functions into 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.




回答3:


The WEEKNUM function considers the week containing January 1 to be the first week of the year. 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.

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

ISO Weeknum UDF is.
The ISO weeknumber

The ISO-criteria for the first week in a year: - a week starts at monday; so monday is the first day of any week - the first week in a year must contain at least 4 days in that year

  • the 1st of january is in week 1 if it's a monday, tuesday, wednesday or thursday
  • the 4th of january is always in week 1 of any year
  • the first thursday in a year is always in week 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 i.e. 9-7-2016, First one gives weeknum 28 whereas ISO weeknum function gives 27

For 9-7-2015 both the above routines will give weeknum 28

To get the current day (without the time portion) in a cell formula in Excel, use:

=TODAY()

In the VBA programming environment, however, the function would be 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.



来源:https://stackoverflow.com/questions/38282079/how-can-i-use-the-weeknum-function-in-a-statement-in-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!