Before Close Multiple Inactive Workbook Close

Bench

Board Regular
Joined
Aug 27, 2009
Messages
134
Hi I wrote a simple function to open 3 workbooks upon opening a workbook,

Then i wrote a simple one to close the 3 workbooks, this works fine as a sub,

However when placed as a Private Sub in the BeforeClose section it doesn't work and tells me subscript is out of range

any ideas?

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Windows("Expenditures.xlsx").Activate
    ActiveWindow.Close
    Windows("Coast Prices.xlsx").Activate
    ActiveWindow.Close
    Windows("Field Service Report.xls").Activate
    ActiveWindow.Close
    Range("A1").Select
End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Hello Bench,
One thing I see is that 'Expenditures' and 'Coast Prices' both have the xlsx file extension while
'Field Service Report' uses the xls extension. Is this correct or a typo?
(I'll assume it is correct for now as you stated that your routine runs fine as a standard sub.)
If that's correct then you might try this in your ThisWorkbook module of the controlling workbook instead.
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
  Workbooks("Expenditures.xlsx").Close False
  Workbooks("Coast Prices.xlsx").Close False
  Workbooks("Field Service Report.xls").Close False
  Range("A1").Select
End Sub
This should close those 3 files from the fourth, without saving them or asking if you want it to.
If you DO want them to save before closing, then simply replace the 3 instances of Close False with Close True
(If you want to be asked if you want any changes saved, then just use 'Close' without any False or True.)
Also note, that we seldom need to activate (or select) an object to work with it. Here we're just referring to them instead of activating them.

Hope it helps.
 
Upvote 0
Hello Bench,
One thing I see is that 'Expenditures' and 'Coast Prices' both have the xlsx file extension while
'Field Service Report' uses the xls extension. Is this correct or a typo?
(I'll assume it is correct for now as you stated that your routine runs fine as a standard sub.)
If that's correct then you might try this in your ThisWorkbook module of the controlling workbook instead.
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
  Workbooks("Expenditures.xlsx").Close False
  Workbooks("Coast Prices.xlsx").Close False
  Workbooks("Field Service Report.xls").Close False
  Range("A1").Select
End Sub
This should close those 3 files from the fourth, without saving them or asking if you want it to.
If you DO want them to save before closing, then simply replace the 3 instances of Close False with Close True
(If you want to be asked if you want any changes saved, then just use 'Close' without any False or True.)
Also note, that we seldom need to activate (or select) an object to work with it. Here we're just referring to them instead of activating them.

Hope it helps.

Hey thanks for this, i managed to get around it by calling a useform but this is much more straightforward. And yes the .xls and .xlsx's come from the joys of working with diff versions of excel at home and work

Thanks for your help
 
Upvote 0
Most welcome. Glad it helped.

EDIT:
If there's any chance a user might close one of your 3 named files before closing the one this routine resides in you might want to think about inserting as your first line of code:
On Error Resume Next
and then
On Error GoTo 0
at the very end.

S'pose I should've thought of that earlier...
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,853
Messages
6,121,935
Members
449,056
Latest member
denissimo

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