On error run subroutine

Mumba

New Member
Joined
May 3, 2018
Messages
14
Hi All

Is it, in any way possible to call a subroutine on any error during a macro?
I created a macro that does some calculations, afterwards data sheets are hidden as veryhidden rendering them invisible for the user.
However on errors the macro breaks and leaves the sheets visible to the user. I am trying to avoid this.

I am thinking something along the lines of this:

On error call SubHideSheets
end
 

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.
Hi,
Possible to manage errors in your routine but would be helpful if you publish the macro that you are having issues with.

Dave
 
Upvote 0
Hi Dave

No problem.
I have several subcodes so i figured it would be more confusing.
What i am looking for i as way to do a total code fail handling. From my understanding On error is i line specific error handling that needs to be above every calculation in the macro, i would very much like to avoid that.
What i would like is this: If at any point the macro breaks down, it should jump to the last subroutine (Call Sheetshide) and execute this before ending the macro (of course be using the "End" functionality)

Code:
Sub Main()

Application.ScreenUpdating = False

Call Definitions

'Set total to 0
wsmaster.Cells(7, "E") = "Please chose type in column ""H"""


Call SheetsUnhide
Call Headlines
Call FindRiisItem
Call ReadyDetails
Call ReadyRefreshCost
Call OptimalChoice

wsmaster.Activate
Call formats
Call Sheetshide

Application.ScreenUpdating = True
End Sub
 
Upvote 0
What error(s) are you trying to handle?
 
Upvote 0
You could try following addition to your code & see if does what you want

Rich (BB code):
Sub Main()


    On Error GoTo myerror
    Application.ScreenUpdating = False
    
    Call Definitions
    
    'Set total to 0
    wsmaster.Cells(7, "E") = "Please chose type in column ""H"""
    
    
    Call SheetsUnhide
    Call Headlines
    Call FindRiisItem
    Call ReadyDetails
    Call ReadyRefreshCost
    Call OptimalChoice
    
    wsmaster.Activate
    Call formats
    
    
myerror:
    Call Sheetshide
    Application.ScreenUpdating = True
    If Err <> 0 Then MsgBox (Error(Err)), 48, "Error"
End Sub

Dave
 
Upvote 0
Any errors that may occour. This a tool for external use. If the user somehow manages to insert a input that will cause an error, i want the macro to hide the datasheets and show a msgbox that asks them to contact a inserted support contact.
I am aware this is a VERY broad approach and that is why i am unsure if it is even possible.
 
Upvote 0
Hi Dave

I seems that it does.
I am actually a bit surprised, when reading about the "on error" functionality it seems that it should not be possible to do it like this.. But it works :)

Thank you
 
Upvote 0
Hi Dave

I seems that it does.
I am actually a bit surprised, when reading about the "on error" functionality it seems that it should not be possible to do it like this.. But it works :)

Thank you

Perfectly possible - save me writing it all out & explaining it, visit Chip Pearson site::http://www.cpearson.com/excel/errorhandling.htm
for full explantion of error Handling

Extract from Chips site relating to your situation below:

Error Handling With Multiple Procedures

Every procedure need not have a error code. When an error occurs, VBA uses the last On Error statement to direct code execution. If the code causing the error is in a procedure with an On Error statement, error handling is as described in the above section.

However, if the procedure in which the error occurs does not have an error handler, VBA looks backwards through the procedure calls which lead to the erroneous code.

For example if procedure A calls B and B calls C, and A is the only procedure with an error handler, if an error occurs in procedure C, code execution is immediately transferred to the error handler in procedure A, skipping the remaining code in B.


Dave
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,201
Members
453,022
Latest member
RobertV1609

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