VBA Script - Auto select end upon macro error msg box & display msg box

Beachson

Active Member
Joined
Oct 28, 2009
Messages
468
Hi all, I have a macro which looks to select a workbook as it's first command ...

Code:
Windows("FileName.XLSX").Activate

If the file is available to select then the macro will run and I have no issues but I am trying to write something that will automatically click "END" upon receiving the "Run time error '9': Subscript out of range" error message box if in fact the workbook is not available to select. Also after auto selecting or confirming "END" I would like to display a message box.

Can anyone help me with this? Many thanks
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
do something like this

Sub MyRoutine

On Error Resume Next

Windows("FileName.XLSX").Activate

If Err.Number = 9 Then GoTo err9

On Error Goto 0

' your code here

Exit Sub

err9:

MsgBox "Workbook not found!"

End Sub

This will trap ONLY the "Subscript Out of Range" error, which occurs when you tell Excel to look at a range, window, worksheet, object etc that does not exist.
 
Upvote 0

Forum statistics

Threads
1,203,387
Messages
6,055,122
Members
444,763
Latest member
Jaapaap

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