Get Actual Date from Day of Week & Week Number

Posted on 2011-05-12
Last Modified: 2012-06-27
Hello Experts,

The attached VBA formula will give you the week number based on two variables (First day of the year and a given date): (Note that Starting day of the year is hard-coded in the formula)

What I need to do is actually to reverse this formula, i.e. to supply week number and Day of week (Sat, Sun, Mon,...Fri) and I get the corresponding date to that day.

For exaple, if I want to know what the date corresponding to Saturday on week 12, the function call will look like the following:

Dim myDate as Date

myDate = GetDate("Sat", 12)
Thank you for your help

Function FiscalWeek(aDate As Date) As Long

Dim yearStart As Date
Dim dayGap As Long, wCount As Long

yearStart = DateSerial(2010, 9, 18)
dayGap = aDate - yearStart

FiscalWeek = Int(dayGap / 7) + 1

End Function

Open in new window

Question by:Mehawitchi
    LVL 85

    Accepted Solution

    Try this:
    Function GetDate(strDay As String, lngWeekNum As Long) As Date
       Dim dteTemp As Date
       dteTemp = DateSerial(2010, 9, 18) + (lngWeekNum - 1) * 7 - 1
       Do Until StrComp(Format(dteTemp, "ddd"), strDay, vbTextCompare) = 0
          dteTemp = dteTemp + 1
       GetDate = dteTemp
    End Function

    Open in new window

    LVL 77

    Assisted Solution

    I assume you have some validation on the day name.

    Function GetDate(wk As Integer, wkday As String) As Date

    Dim yearStart As Date
    yearStart = DateSerial(2010, 9, 18)
    yearStart = yearStart + (wk - 1) * 7
    yearStart = yearStart + Nz(Switch(wkday = "Sat", 0, wkday = "Sun", 1, wkday = "Mon", 2, wkday = "Tue", 3, wkday = "Wed", 4, wkday = "Thu", 5, wkday = "Fri", 6), 0)
    GetDate = yearStart
    End Function


    Author Closing Comment

    Solution by rorya is working 100%
    Solution by  peter57r has a small bug:
    "Nz" was not recognized by the VBA compiler and the function halted there, but I appreciate the effort
    LVL 85

    Expert Comment

    by:Rory Archibald
    Nz is an Access function and will work there (but not elsewhere).

    Author Comment

    I'm using this function in Excel - That's why.

    Thanks for clarifying
    LVL 77

    Expert Comment

    True- I was conscious of it but forgot to change the code in the end.  I made the comment about validating the day, because I knew that nz would not be available-  - but anyway, you have a working solution.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
    Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
    The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now