In trying to help out here wherever I can I notice that members who are far better at Excel vba than me will post code that does things like alter (turn off) application settings but they don't use an error handler in that code. Nor do they suggest that their code should be modified to include one. I'm trying to understand why those members don't use one or advocate it. I have copied/pasted code that the OP says raises an error and found that it does. Then I notice that when the procedure terminates (I click OK to the error message), my app settings remain altered. Here's an example of the type of altered settings I'm referring to
If you resume using the workbook, do you not risk doing so while things are likely altered in a way that might be detrimental (e.g. events are disabled)? The only way things will get back to normal is if the problem procedure (or one like it) eventually runs to its completion. Why is it that I don't see anyone else recommending an error handler that will ensure these settings are restored to normal if the procedure terminates early due to an error?
Perhaps to clarify what I'm saying ought to be done, review the following pseudo code.
TIA
VBA Code:
Application.EnableEvents = False
Application.Calculation = xlCalculationManual = False
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Perhaps to clarify what I'm saying ought to be done, review the following pseudo code.
VBA Code:
Some Sub()
'Declarations
On Error GoTo errHandler
'some code next
exitHere: 'a line label
With Application
.EnableEvents = True
.Calculation = xlCalculationManual = True
.ScreenUpdating = True
.DisplayAlerts = True
End With
Exit Sub 'don't forget this line!!
errHandler:
' do stuff to handle errors
Resume exitHere 'go to line after exitHere line label, which will restore the altered app settings
End Sub
TIA