If Activate Command Results In Run Time Error, Then Test a Second Option

jbumps

New Member
Joined
Dec 18, 2012
Messages
10
All,

Apologies for the vague title - could not find a good way to word it. This thread plays off a recently posted one that a member was very quick to answer and get me on my way.

I have a file name of "May Scrap Net of Reserve with scrap codes.xls". My earlier post centered around how to automatically define "FileName" as the current month, solved in this manner:

Code:
FileName = Format(Now(), "mmm ") & "Scrap Net of Reserve with scrap codes"
Windows(FileName).Activate
[CODE]

My question this go around is: The month portion of the file refernce will still be "May" for a the first trip through the daily update to the file, in early June. This will result in a Run Time Error if the user doesn't modify the code. 

Is there a way to tell the program to, if the above code doesn't reference a valid file, to adjust the month backward one month?

Example:

It's Monday June 3rd 2013. That Monday I will need to run the scrap file for the month of May one more time (as I'm always capturing the previous workday's data), but the macro runs and doesn't find the "May" file, as the system clock/bios tells the program it's June. 

Thanks to anyone who can point me in the right direction on this one!
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Yes, use
Code:
On Error Goto
Don't forget to reset the error behaviour after this, see example below:
Code:
Sub test()
    On Error GoTo LastMonth
    Windows("Book2.xls").Activate
    On Error GoTo 0
    
    Exit Sub
    
LastMonth:
    MsgBox "Book2 is not open"
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,794
Members
449,095
Latest member
m_smith_solihull

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