Error Handler - Close Workbooks if open

mmetzinger

Board Regular
Joined
Dec 30, 2010
Messages
61
Can someone explain to me why excel won't let me do this? What I want is for any of my three workbooks that might be open to be closed upon macro error. Testing this doesn't work as excel yells because during testing I don't open one file to make the error happen.

Bunch of code that leads to this statement at the end..

Code:
Exit Sub

errhandler:
    On Error Resume Next
    Application.DisplayAlerts = False
    Windows("RMCensus.xlsx").Close
    Windows("Iservices.xlsx").Close
    Windows(outputwb).Close
    MsgBox ("An error occured while running the macro." & vbCrLf & "Please report to BCI IT.")
    
End Sub

Any thoughts?
 
Last edited:

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Try

Workbooks( ) instead of Windows( )

Also, to be clear, are the workbooks opened or not, before attempting to close?
And why would you get an error, you are using On Error Resume Next.
 
Upvote 0
Try

Workbooks( ) instead of Windows( )

Also, to be clear, are the workbooks opened or not, before attempting to close?
And why would you get an error, you are using On Error Resume Next.


I will try workbooks instead of Windows today. The first line of code in my project is On error goto errhandler. The part I posted is just the errhandler portion at the bottom of my module. What I need is if the workbooks are open for them to be closed but depending on where the code breaks I do not know if the workbook is open or not yet. Hence why I have the updated error handler of On error resume next.
 
Upvote 0

Forum statistics

Threads
1,215,200
Messages
6,123,604
Members
449,109
Latest member
Sebas8956

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