[VBA] How to handle errors when adressing multiple sheets

NessPJ

Active Member
Joined
May 10, 2011
Messages
416
Office Version
  1. 365
Hi,

I am currently working on an Excel sheet with VBA that also opens a few other files in the routine etc.

Now i was working on creating an ErrorHandler for the file but i ran into a problem where my "OriginalWorkbook" was no longer
activated because i opened "WorkbookX" and an error would occur.

What would be my best way for the ErrorHandler to safely check/handle Activating the "OriginalWorkbook" and or even closing "WorkbookX" ?

If i use the following construction inside the ErrorHandler when it is called, RTBook could actually be the name of "WorkbookX" instead of "OriginalWorkbook".

RTBook = ActiveWorkbook.Name

Application.Workbooks(RTBook).Activate

Eg. The filename for "OriginalWorkbook" is not a constant and changes on a weekly basis (otherwise i could declare it as a Public Constant).

Is there something like a Public Variable i can use or something?
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
'ThisWorkbook' is the workbook where the running VBA code is housed. That should help.
 
Upvote 0

Forum statistics

Threads
1,214,593
Messages
6,120,434
Members
448,961
Latest member
nzskater

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