VBA looping range of cells then Get a Day from a WeekNumber

geloader0

Board Regular
Joined
Dec 21, 2022
Messages
52
Office Version
  1. 2016
Platform
  1. Windows
Hi Good day, I want to convert the Weeknumber to a date example from Week 1(start from Sunday), Month 1(January) then year. So the output goes like this " 01/01/2023 - 01/07/2023 "
Please see example below... Thank you very much

Convert Weeknumber from this

1675544502188.jpeg


To this.

1675544540455.jpeg
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Your description says you want Week 1 to start on Sunday, but your table shows Week 2 in 2022 starting on Saturday. This raises two questions...

1) Does Week 1 start on Sunday or the 1st of January?

2) If it starts on Sunday, what should be shown for Week 1 in 2022 which, for that assumption, would actually be 12/26/2021-1/1/2022?
 
Upvote 0
Your description says you want Week 1 to start on Sunday, but your table shows Week 2 in 2022 starting on Saturday. This raises two questions...

1) Does Week 1 start on Sunday or the 1st of January?

2) If it starts on Sunday, what should be shown for Week 1 in 2022 which, for that assumption, would actually be 12/26/2021-1/1/2022?

Hi! Sorry, I was mistake the Days always starts from Sunday. So, for the Week 2 of 2022 it must be 01/09/2022 - 01/15/2022
#1. The Week 1 start from 1st of January and the day start from Sunday to Saturday
#2. I didn't notice that there is only 1 day in Week 1 in 2022. Or maybe the Week 1 in 2022 will be 01/02/2022 - 01/08/2022 , If 12/26/2021 - 01/01/2022 is difficult to get.
 
Last edited:
Upvote 0
#2. I didn't notice that there is only 1 day in Week 1 in 2022. Or maybe the Week 1 in 2022 will be 01/02/2022 - 01/08/2022 , If 12/26/2021 - 01/01/2022 is difficult to get.
I can get either one of those for Week 1, you just have to tell me (us) which is the one you want.
 
Upvote 0
I can get either one of those for Week 1, you just have to tell me (us) which is the one you want.

Anyway, I found this code to somewhere

THE FUNCTION to get the date within that weeknumber and year range
Excel Formula:
Public Function GetDayFromWeekNumber(InYear As Integer, _
                WeekNumber As Integer, _
                Optional DayInWeek1Monday7Sunday As Integer = 1) As Date
    Dim i As Integer: i = 1
    If DayInWeek1Monday7Sunday < 1 Or DayInWeek1Monday7Sunday > 7 Then
        MsgBox "Please input between 1 and 7 for the argument :" & vbCrLf & _
                "DayInWeek1Monday7Sunday!", vbOKOnly + vbCritical
        'Function will return 30/12/1899 if you don't use a good DayInWeek1Monday7Sunday
        Exit Function
    Else
    End If

    Do While Weekday(DateSerial(InYear, 1, i), vbMonday) <> DayInWeek1Monday7Sunday
        i = i + 1
    Loop

    GetDayFromWeekNumber = DateAdd("ww", WeekNumber - 1, DateSerial(InYear, 1, i))
End Function

This code will give me the exact date I want in Week 2 of 2022 then adding 6days to the value
Excel Formula:
Sub TEST()
Msgbox Format(GetDayFromWeekNumber(Range("B3"), Range("A3"), 7), "MM/dd/yyyy") & " - " & Format(GetDayFromWeekNumber(Range("B3"), Range("A3"), 7) + 6, "MM/dd/yyyy")
End Sub

1675565553571.png


Now, I want to loop through cell range in column A and B
 
Upvote 0

Forum statistics

Threads
1,214,784
Messages
6,121,536
Members
449,037
Latest member
tmmotairi

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