Error handler close workbook if opened during code

rhombus4

Active Member
Joined
May 26, 2010
Messages
488
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
 

rhombus4

Active Member
Joined
May 26, 2010
Messages
488
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Sorry one quick question Currently if no errors
I have

VBA Code:
On error resume next
 wb.close savechanges: = True
On error goto 0

Is it better to use your code and add save changes

VBA Code:
If Not wb Is Nothing Then wb.Close savechanges: = True
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,252
Office Version
  1. 365
Platform
  1. Windows
I would check to see if the workbook exists rather then using the on error, but that's more personal preference.
 

rhombus4

Active Member
Joined
May 26, 2010
Messages
488
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Check workbook exists??

Is that what your code checks for
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,252
Office Version
  1. 365
Platform
  1. Windows
I didn't phrase that very well, it checks to see if wb has been set
 

rhombus4

Active Member
Joined
May 26, 2010
Messages
488
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
AHH ok. so your code to check if workbook has been set and adding savechanges: = true can be used instead of

On error resume next
wb.close savechanges: = True
On error goto 0
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,252
Office Version
  1. 365
Platform
  1. Windows
That's right. :)
 

Forum statistics

Threads
1,141,059
Messages
5,704,034
Members
421,323
Latest member
Exidous

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
Top