WeekNum function not returning correct value

AndyEd

Board Regular
Joined
May 13, 2020
Messages
124
Office Version
  1. 365
Platform
  1. Windows
Can someone please help. I have the following which I am using to calculate the Week Number is respect of the current date, Today().

VBA Code:
With ThisWorkbook.Worksheets("Tracker") ' Select workbook and sheet.
        .Cells(.Rows.Count, "M").End(xlUp).Offset(0, 1) = Application.WorksheetFunction.WeekNum(ThisWorkbook.Worksheets("Tracker") _
        .Cells(.Rows.Count, "M").End(xlUp).value - WeekNum(Date))
    End With

I seem to be having an issue with the 2nd half of the function, from " -WeekNum(Date())". When displayed as above it returns the following error:

Comile error:
Sub or Function not defined

If I change to " - Application.WorksheetFunction.WeekNum(Date())" for a date of 29/01/2024 it returns "4," rather than "-3", which is what I am hoping to get.

As always, with appreciation.
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Hi, you have a misplaced parentheses:

Rich (BB code):
With ThisWorkbook.Worksheets("Tracker") ' Select workbook and sheet.
    .Cells(.Rows.Count, "M").End(xlUp).Offset(0, 1) = Application.WorksheetFunction.WeekNum(ThisWorkbook.Worksheets("Tracker") _
    .Cells(.Rows.Count, "M").End(xlUp).Value) - Application.WorksheetFunction.WeekNum(Date)
End With

You could also use the VBA's native DatePart() function to get the week, for example:

VBA Code:
With ThisWorkbook.Worksheets("Tracker")
    With .Cells(.Rows.Count, "M").End(xlUp)
        .Offset(0, 1).Value = DatePart("WW", .Value) - DatePart("WW", Date)
    End With
End With
 
Upvote 1
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top