Error trap mishap

NdNoviceHlp

Well-known Member
Joined
Nov 9, 2002
Messages
3,629
I'm having some trouble understanding what's going on with this error trap
<pre>Sub test()
On Error GoTo correct
userform1.Show
exit sub
correct: MsgBox "U messed up": Unload userform1: userform2.Show
End Sub</pre>
This code is just to illustrate an example. After having an error in "test", when I use a command button on userform2 to unload userform2 it takes me back to this error trap...I don't get it?
editx2: code correction
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Thought maybe I would provide a bit more info. The error I'm addressing is a divide by zero error in "test" when the user fails to enter data which is to be manipulated within 'test'. I am returned to the error message and loading userform2 only if an error has occurred. It also seems that I am returned to this message etc. for every occurrence of an error...ie when I use the userform2 command button to unload userform2, if 3 errors have occurred I get the error message etc 3 times..I find this very puzzelling?
 
Upvote 0
Hi,

As Phantom has indicated, you really should post the actual code that you are using so that people can advise you where it is going wrong.

However, in general terms you should try to follow a certain structure with your error-checking. This includes resetting the error-checking so that Excel takes over again and making sure that your custom routine is only run when there is an error. Have a look at the code below :
Code:
Sub test()
Dim i As Integer

On Error GoTo correct
' use custom error-checking
i = 50000
' the bit that might error (integer can't exceed 32,767)
On Error GoTo 0
' reset error checking to normal
'
' the rest of your code
'

Exit Sub
'need to make sure error routine is not run at the end of the sub
correct: MsgBox "U messed up"

End Sub
HTH
 
Upvote 0
The help is much appreciated. I'm pretty sure I see a couple of things that I didn't include in my code that are likely important. Re-setting the error check to normal..sounds nb but I don't know how do to that?...and I can see where exiting a sub before the error correction was probably my downfall ...I maybe hadn't actually left that subroutine before showing userform2 then unloaded it ? For each error I think I had to go back and complete the sub?...which come to think about it may have some obscure use? If anyone can further help with any more information on error correction I'm very interested in knowin it.Dave
ps thanks Phantom..I would post the code..long and convaluted wouldn't help...apologies for the poor code example.
edit: on error goto exit Is this the custom format that I'm missing?
 
Upvote 0
Hi Dave,

With regard to resetting the error-checking, this is achieved by the line On Error GoTo 0 (as indicated in the example above :wink: )

And as regards further help on the subject - have you looked at On Error Statement in the VBE Help? Its going to be a little tricky for anyone to provide any more specific help on the issue if you are not able to illustrate the nature of your problem.
 
Upvote 0
Thanks for your assistance and patience Ritchie. Maybe the correct use of a 2X4 across my noggin would reset my thinking and let me see the error of my ways. The missing exit I see was very nb. I editted my initial post to what now works. I still have some room left for another lump if anyone can explain why in the above code, on correct, if you reverse the msgbox and unload/load components the msgbox doesn't get displayed until you try to unload userform2 with the userform2 command button and again it comes up once for each error. Presently, it returns you to the worksheet to dispay the msgbox, then on OK it displays userform2...I would prefer to avoid returning to the worksheet. I also probably should have mentioned that "test" is actually code for a command button on userform2 which may causing this recursive problem? The VBE help on this topic was also very useful. Thanks again. Dave
 
Upvote 0
I took the easy out and included an If textbox control scource(s) = 0 then error msg else do the code which does what I was after. I've learned quite abit about errors along the way. I am still unclear on this focus thing. The error handler: userform2.show : msgbox sends the focus to userform2 and has no way of completing the msgbox part of the code until the command button from userform2 unloads userform2? With each error it creates another occurrence? There must be something more to error handling that I'm missing if anyone has a stick.
 
Upvote 0

Forum statistics

Threads
1,215,352
Messages
6,124,449
Members
449,160
Latest member
nikijon

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