week2date function different result on two machines

phairplay

Active Member
Joined
Nov 2, 2011
Messages
260
Hi Guys,
I'm using the below function which turns a week number into a week


Code:
Public Function Week2Date(WeekNo As Long) As Date
     Dim Jan1             As Date
     Dim Sub1             As Boolean
     Dim Ret              As Date
    Jan1 = DateSerial(Year(Date), 1, 1)
     'Sub1 = (VBA.Format(Jan1, "ww", VBA.vbMonday, VBA.vbFirstFourDays) = 1)
     Sub1 = (Format(Jan1, "ww", vbUseSystem, vbUseSystem) = 1)
     Ret = DateAdd("ww", WeekNo + Sub1, Jan1)
     Ret = Ret - WeekDay(Ret) + 7
     Week2Date = Ret
 End Function


I use it to return the date 25 weeks from now
Code:
dateweek = DateValue((Week2Date((Format(Now, "ww") + 25))))
when I load the macro on another machine I noticed I was getting a different date

when running the following:
Code:
dateweek = DateValue((Week2Date((Format(Now, "ww")))))
my machine returned the correct result which is Week 45 yet on my other machine it returns a result of week 46

could someone please help me find out what is causing this issue?

many thanks
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
I guess the logical reason is this:

Code:
     Sub1 = (Format(Jan1, "ww", vbUseSystem, vbUseSystem) = 1)

If the two different machines have different system settings for first day or week / first week of year then you could get different results.

WBD
 
Upvote 0
hi thank you for the response,
I've checked and both machines have the same region, date and time

one machine is windows 7 (the one with the correct week) the other is windows 10

is there away to fix this?
 
Upvote 0
Not sure your calculation is correct anyway; Week2Date(1) gives 14th January? How about this function instead?

Code:
Public Function Week2Date(weekNumber As Long) As Date

Dim testDate As Date

Week2DateWBD = DateSerial(Year(Date), 1, 1)
testDate = Week2DateWBD - Weekday(Week2DateWBD, vbMonday) + (weekNumber - 1) * 7 + 1
If testDate > Week2DateWBD Then Week2DateWBD = testDate

End Function

WBD
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

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