Error handler close workbook if opened during code

rhombus4

Well-known Member
Joined
May 26, 2010
Messages
586
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
During my code if there is an error it jumps to errorhandler which will close a workbook if it was opened during the code. If it wasn't opened it can just ignore it
However I get run time error 9 subscript out of range on the set wb line in eh:

VBA Code:
Sub test()
On error goto eh
Code

Dim wb as workbook
Set wb = workbooks.open("c:\test.xlsx")

Some more code

Exit sub

eh:
On error resume next
Set wb = workbooks("test.xlsx")
wb.close
On error goto 0

End sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
How about
VBA Code:
eh:
   If Not wb Is Nothing Then wb.Close
End Sub
 
Upvote 0
Solution
Does your one line of code replace everything I had before. does it not need an on error resume next before it.

Also, If an error was caused before the
Set wb = workbooks.open("c:\test.xlsx")
Do I need to declare again before the
If Not wb Is Nothing Then wb.Close

As It doesn't close the workbook
 
Upvote 0
Just replace this
VBA Code:
eh:
On error resume next
Set wb = workbooks("test.xlsx")
wb.close
On error goto 0

End sub
with the code I suggested.
 
Upvote 0
But if the test workbook was already open it won't close if an error happened before it was set

E.g.

VBA Code:
Sub test()
On error goto eh
X  = 1/0

Dim wb as workbook
Set wb = workbooks.open("c:\test.xlsx")

Some more code

Exit sub

eh:
If Not wb Is Nothing Then wb.Close
End sub
 
Upvote 0
Why would you be opening a workbook if it was already open?
 
Upvote 0
Why would you be opening a workbook if it was already open?
Good point lol

PS, I didn't understand how it could close wb if wb hadn't been set as the error didn't get that far and jumped to eh:

It's more a case of if the code opens and closes multiple workbooks and an error occurred. Could be anywhere

So Would it just be
Eh:
If Not wb1 Is Nothing Then wb1.Close
If Not wb2 Is Nothing Then wb2.Close
If Not wb3 Is Nothing Then wb3.Close

And would close wb1 wb2 wb3 if they were opened and ignore if they weren't
 
Upvote 0
That's right, as you are setting the variable when the workbook is opened, if the code fails before a workbook is opened then wb1 will be Nothing
 
Upvote 0
Thanks Fluff
So many different answers online which didn't work for me. Yours seemed to work ??

Marked your first reply as solved
 
Upvote 0
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,424
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