Date formats in VB question

KillGorack

New Member
Joined
Jan 23, 2006
Messages
35
Office Version
  1. 2016
Platform
  1. Windows
I have a week number (ISO), and a year. I need to get a date in mm/dd/yyyy on the Monday

for example a system we use provides this;
202308

And would love a bit of code to turn that into
02/20/2023

Not in a cell but with VB code

I'm wracking my brain...
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Try:

VBA Code:
debug.Print Format(202308,"mm/dd/yyyy")

Edit: Oops, sorry, I thought that was a date serial. Let me take another look.
 
Upvote 0
Maybe:
VBA Code:
Sub GetDate()
Dim Inp, wkNum As Long, Yr As Long, Dte
Inp = InputBox("Enter Input in form yyyyWeekNumber")
Yr = Left(Inp, 4)
wkNum = Right(Inp, 2)
Dte = Evaluate("DATE(" & Yr & ", 1, -3 + 7 *" & wkNum & "- WEEKDAY(DATE(" & Yr & ", 1, 4), 2) + 1)")
MsgBox Format(Dte, "mm/dd/yyyy")
End Sub
 
Upvote 0
Solution
I had to do some digging into the ISO standard. I see Joe beat me with the code, but I get some different results with my formula. If Joe's doesn't work for you, try change the Evaluate line to:

VBA Code:
Dte = Evaluate("DATE(" & Yr & ",1,1)+CHOOSE(WEEKDAY(DATE(" & Yr & ",1,1)),0,-1,-2,-3,-4,2,1)") + (wkNum - 1) * 7

This ensures that the date returned is always a Sunday.
 
Upvote 0
I have a bunch of dates to check, once I check dates from lists with a few years worth of dates I'll come back.

Not the resolution i expected.. thought excel would have something canned.. but it looks promising..
 
Upvote 0
My sub returns a Monday date per the OP - "I need to get a date in mm/dd/yyyy on the Monday"
 
Upvote 0
Joe's code correlates perfect on the Monday of the week. It's perfect. I'll make a function for this one for sure I use it everywhere..

I appreciate you all!
 
Upvote 0
This is all you need! ;)

Based on the ISO-fact that every first thursday has to be in the new year

VBA Code:
Function jec(year As Integer, week As Integer) As Long
jec = 7 * (week - 1) + DateSerial(year, 1, 4) - Weekday(DateSerial(year, 1, 4), 2) + 1
End Function

Excel Formula:
=jec(A1,A2)

If you want to know the date of an other day of the week: (change the day parameter):
This one below is also adapted to your situation.

VBA Code:
Function jec(cell As Variant, day As Integer) As Long
jec = 7 * (Right(cell, 1) - 1) + DateSerial(Left(cell, 4), 1, 4) - Weekday(DateSerial(Left(cell, 4), 1, 4), 2) + day
End Function

Excel Formula:
=jec(A1,1)
 
Last edited:
Upvote 0
Joe's code correlates perfect on the Monday of the week. It's perfect. I'll make a function for this one for sure I use it everywhere..

I appreciate you all!
Glad we could help.
 
Upvote 0
One small change:

VBA Code:
Function jec(cell As Variant, day As Integer) As Long
jec = 7 * (Right(cell, 2) - 1) + DateSerial(Left(cell, 4), 1, 4) - Weekday(DateSerial(Left(cell, 4), 1, 4), 2) + day
End Function

Excel Formula:
=jec(A1,1)
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,199
Members
449,072
Latest member
DW Draft

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