Amend Code

Posted on 2014-08-05
Last Modified: 2014-08-06
I have some code which im running which i do not fully understand.

My aim is to open a folder, thats always last month, so this month it will open July

On the 1st August, this was correct, but today it is looking for an August folder, therefore debugging as it does not exist

My suspicion is it is due to the line If Day(Date) = 1, can i remove that, is this necessary?

Any time of the month, i always want to search for the previous month


    FileYear = Year(Date)

    MonthOffset = 0
If Day(Date) = 1 Then MonthOffset = 1
    FileMonth = Month(Date) - MonthOffset
    FileDate = Format(Date - 1, "yyyymm")
    FilePath = "R:\SPM\Monthly_Inputs\" & FileYear & "\" & FileDate & "\Reporting\PCM_Cross_Region_Lost_Raw.xlsx"

    Workbooks.Open (FilePath)

Open in new window

Question by:Seamus2626
    LVL 26

    Expert Comment

    Try this:
        FileYear = Year(Date)
        FileMonth = Month(DateAdd("m", -1, Date))
        FileDate = Format(Date - 1, "yyyymm")
        FilePath = "R:\SPM\Monthly_Inputs\" & FileYear & "\" & FileDate & "\Reporting\PCM_Cross_Region_Lost_Raw.xlsx"
        Workbooks.Open (FilePath)

    Open in new window

    LVL 26

    Accepted Solution

    btw, I don't see where you use FileMonth.

    Author Closing Comment

    I used   FileDate = Format(Date - 15, "yyyymm"), that covers me ;-)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
    This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
    This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
    This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

    737 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

    22 Experts available now in Live!

    Get 1:1 Help Now