Home » excel » vba – Opening file based on the current week

vba – Opening file based on the current week

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have a workbook that for example sake we will call Monthly Dashboard, the information in this is created entirely from a separate workbook named Trial Balance, barring one cell. This data is going to be collected from a weekly workbook called Project Control. Project control files are saved in the format of YYYYWProjectControl.xlsx, where YYYY refers to the current year and W refers to the week.

The cell that needs to be filled is the total sales figure of all projects to date on the weekly form. What I need to do is programatically open the last created project control and take this sales figure out and close/save.

The closing and saving is all completed as part of a previous project but I need to know how to check what the current week number is and to check which the last created file is. Does anyone have any clue how to do this? If so please also bear in mind that with the end of the year approaching that January will need to search for the last created 2013 project control and if there is not one then it will need to search 2012.

Thanks in advance

How to&Answers:

Depending on whether you are referring to actual week or Fiscal (Financial) week then you can use either of these two functions to calculate for you:

Fiscal Week:

Function FiscalWeek(dtDate As Variant)

Dim dtFiscal    As Date
Dim dtFstMon    As Date
Dim i           As Integer

    'Ensure we are working with a valid date
    If Not IsDate(dtDate) Then
        FiscalWeek = "Invalid Date"
        Exit Function
    End If

    'Strip out any timestamps
    dtFiscal = DateSerial(Year(dtDate), Month(dtDate), Day(dtDate))

    'Determine first Monday of April for given year
    i = 1
    Do Until Weekday(DateSerial(Year(dtFiscal), 4, i), 2) = 1
        i = i + 1
    Loop

    dtFstMon = DateSerial(Year(dtFiscal), 4, i)

    'Work out the difference in weeks from April for the relevant year
    Select Case dtFiscal
        Case Is < dtFstMon
            FiscalWeek = DateDiff("WW", DateSerial(Year(dtFiscal) - 1, 4, 1), dtFiscal, 2)
        Case Else
            FiscalWeek = DateDiff("WW", DateSerial(Year(dtFiscal), 4, 1), dtFiscal, 2) + IIf(i = 1, 1, 0)
    End Select

End Function

Or Actual Week:

Function ActualWeek(dtDate As Variant)

Dim dtActual    As Date
Dim dtFstMon    As Date
Dim i           As Integer

    'Ensure we are working with a valid date
    If Not IsDate(dtDate) Then
        ActualWeek = "Invalid Date"
        Exit Function
    End If

    'Strip out any timestamps
    dtActual = DateSerial(Year(dtDate), Month(dtDate), Day(dtDate))

    'Determine first Monday of January for given year
    i = 1
    Do Until Weekday(DateSerial(Year(dtActual), 1, i), 2) = 1
        i = i + 1
    Loop

    dtFstMon = DateSerial(Year(dtActual), 1, i)

    'Work out the difference in weeks from January for the relevant year
    Select Case dtActual
        Case Is < dtFstMon
            ActualWeek = DateDiff("WW", DateSerial(Year(dtActual) - 1, 1, 1), dtActual, 2)
        Case Else
            ActualWeek = DateDiff("WW", DateSerial(Year(dtActual), 1, 1), dtActual, 2) + IIf(i = 1, 1, 0)
    End Select

End Function