On Error Help - Sub calling Private Subs

mmetzinger

Board Regular
Joined
Dec 30, 2010
Messages
61
Ok, I have never done anything with On Error before so please view me as a total newb in that regard. What I am trying to figure out is if I have a main Sub that is then calling a user form that then calls a private sub do I only need the On Error statement in the start Sub?

Here is the basic layout to my code minus the meat as their is a lot:

Code:
Option Compare Text
Public SelectedAccount As String
Public Accountcell As String
Public CensusType As String
Public ShortName As String
Public emailto As String

Sub CRM_Check_Account()

CensusTypeForm.Show

End Sub

This then goes to the userform and after the user makes their selection it returns to the next private sub

Code:
Private Sub CRM_Check_Cont()
    AccountsForm.Show
End Sub

There are multiple forms and private subs so this is just the first small bit.

What I want is an On Error statement that simply tells the user to contact the IT department if the macro errors out at all and then closes some reference workbooks that it may have opened.

Any ideas?
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
showing a form breaks the call stack (if you view it you'll see 'non-basic code' so errors will not be passed back to an error handler in the routine that called the form as long as the form is loaded. any routines you call directly from the form will need their own error handler
 
Upvote 0
showing a form breaks the call stack (if you view it you'll see 'non-basic code' so errors will not be passed back to an error handler in the routine that called the form as long as the form is loaded. any routines you call directly from the form will need their own error handler

Thank you! That is what I needed to know
 
Upvote 0
Ok, so now I have the follow error handler in my code--

Code:
On Error GoTo ErrHandler:
...
Exit Sub
ErrorHandler:
MsgBox ("An error has occured, please report to IT.")
Windows("CRMAccounts.xlsx").Close

My issue is I want the the workbook listed to close IF IT IS OPEN but since the macro could possibly error out before it is opened I run into another problem. When it hits the close command and the workbook isn't open it generates an excel "Debug or End" message which is what I don't want happening. So i need some code that will say if the workbook(s) are open they need to be closed in the error handler event. My overall goal is to make it where the end user never ever has the opportunity to see/modify the VBA code due to an error message bring up the editor.
 
Upvote 0

Forum statistics

Threads
1,215,529
Messages
6,125,343
Members
449,219
Latest member
Smiqer

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