MrExcel Publishing
Your One Stop for Excel Tips & Solutions

Coming out of macro with error message

Posted by yogesh k. potdar on October 31, 2001 12:33 PM


I would like to the macro to stop and come out with a warnign message that - say "Cannot run on this file, check file contents" - if its unable to process the file - because of various reasons - e.g. data format not correct or divide by zero or something else..
Basically a generic way in which macro "stops" when it encounters error or cannot proceed rather than just

It would be great if someone can suggest how to know the error codes - i.e. macro stops with "OVerflow" or
"Bad Data type" or something like that..


Posted by Jerid on October 31, 2001 12:44 PM

Add this to your macro.

sub YourMacro()
'First line of code
On Error Goto ErrHandler

'Your code

'Bottom of sub
Exit sub

Msgbox "An Error has occured" & vbcrlf & "Error Number " & Err.number
exit sub
end sub

Posted by yogesh k. potdar on October 31, 2001 1:25 PM

Thanks to everyone for working on this for me the answer that Juan Pablo gave worked,what was that code 160?? now any ideas why I can’t do the Alt 0,1,6,0 in the replace box? I will try it on my computer at work tomorrow and see if it works. Do you think reinstalling Excel might help? Again thanks to everyone for your help. Paul B