On error, can display sub that was running in msg box?

cbrf23

Board Regular
Joined
Jun 20, 2011
Messages
241
I would like to know if its possible, when an error occurs, to get the name of the sub that contains the code that generated the error?

Also, if its possible to get the line of code would be great too!

I would like to use the info to display it in messagebox when an error occurs.

Something like:
Code:
on error goto errhandler
[COLOR=sienna]'some code that generates an error[/COLOR]
errhandler:
dim myerr as classmyerr
set myerr = new classmyerr
msgbox "error while running sub: " &[COLOR=green] [I]myerr.sub 'get name of sub where error occured [COLOR=black]_[/COLOR][/I][/COLOR]
             & vbcrlf & "at line: " & [I][COLOR=green]myerr.line 'get line number where error occurred[/COLOR][/I]
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
You need to program it into your error handler code. Here's something I use that gives me the worksheet associated with the code as well as what code routine was being processed at the time of the error:

Code:
ErrHandling:
    If Err.Number <> 0 Then
        MsgBox "Error occurred during Worksheet_SelectionChange." & vbCrLf & vbCrLf & _
               "Error #" & Err.Number & vbTab & Err.Description, vbInformation, _
               ActiveWorkbook.ActiveSheet.Name & Space(3) & "Worksheet_SelectionChange"
    End If
 
Upvote 0
If you use MZ Tools, it has that option built in to its error handler template.
 
Upvote 0
@ doofusboy:
I know I could hard code it into each routine, but I have a lot of subs that run that call propertys of classes I have written, or run other subs and pass variables....it would be very tedious (and makes the code longer and harder to read) to write repetetive error handlers for each one, just to add the sub routine name.

VBA's default error behavior highlights the code in the sub that the error occured in, so the info has to exist in order for VBA to do this....i'm just wondering if its possible to extract this info?

@ Rorya:
If it was for personal use, I might consider something like that, but I need this for code I'm writing at work, and I cant install any third party software....

But, could I trouble you to open up the error handler template you mentioned, with that functionality built in, and paste the code here so I could see it?
 
Upvote 0
It won't work for you since the template is then translated by MZ Tools to input the relevant info. My version looks like:
On Error GoTo {PROCEDURE_NAME}_Error

{PROCEDURE_BODY}

clean_up:
On Error GoTo 0
Exit {PROCEDURE_TYPE}

{PROCEDURE_NAME}_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure {PROCEDURE_NAME} of {MODULE_TYPE} {MODULE_NAME}"
Resume clean_up
 
Upvote 0
I see...I wonder how they pull that information?

Anyways, I ended up going with a form of central error handling.
I defined a few global variables for my module name and sub name.
Then I created a module called handleErrors and wrote a sub called errorHandler which does everything I want to do in the event of an error.
Then in my error handler in all my subs, I just define the module name and sub name variables and run my errorHandler sub.

Its not the perfect solution I was hoping for, but it is a lot easier to read and a lot less tedious to maintain then having the same errorhandler in multiple sub routines...

I did find out that its possible to get the current sub/module/and project by accessing the vbe object, but you have to enable programmatic access to visual basic. Since I will be deploying this across mulitple workstations, that is not a viable option for me...

Anyways, thanks for the help guys!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,280
Members
452,902
Latest member
Knuddeluff

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