Exit Sub causing excel to crash

cbrf23

Board Regular
Joined
Jun 20, 2011
Messages
241
I have a routine, its pretty lengthy and I dont know that its necessary to paste the code to describe the issue. I will if need be...

Anyways, there are basically two procedures that get run here. The the calling procedure gets some info, and creates an object of a custom class. The custom class runs one procedure, it exports all sheets in the workbook to the chosen directory in their own workbook and saved in their own folder. When its done it exits back to the calling procedure, and then the calling procedure ends (Exit Sub).

Everything works. If I step through the code using F8, everything works flawlessly. But, if I let the code run, when it tries to exit back to the calling procedure, excel crashes. Everytime. Without fail.

Now, whats wierd, is that If I let the code run on its own all the way up to "Exit Sub" line of the running procedure in the class module (I put a break line on this line), everything runs fine, and if I hit F8 to execute the "Exit Sub" line, no crash!

I even put message boxes in after each line of code at the end of both procedures, and let the code run on its own, so that I could pinpoint the exact line causing the crash. This confirmed it to be "Exit Sub" in the class module.

WTF? Anyone have any ideas why everything works pefectly if I hit F8 to execute that line of code, but not if I let the program run itself????

I'm very annoyed by this....Any help will be greatly appreciated!
 
Last edited:
In your original (crashing) code, there is a procedure you call right about where it crashes called myMsgBox. I cannot find this procedure in your class.

Code:
    MyMsgBox _
    Prompt:=strReport, _
    Buttons:=vbOKOnly, _
    Title:="Export report"
 
Upvote 0

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
You've also got an undimmed variable oBar. I don't see how this class can be option explicit when variables aren't declared - are you using public variables?
 
Upvote 0
The MyMsgBox procedure is actually what I changed to after I got everything fixed.... MsgBox is what that should be - its what I was using when I was getting crashing issues... I tried to replace all those before posting the code on here, looking back, it looks like I replaced them all in the fixed code but not in the original (bad) code. LOL.

As for the oBar reference, yes, that is a public variable. Its a progress bar I created -I use it in a lot of projects... its a userform with 2 labels and one frame. The first label shows text which can be updated, and the second label is colored and changes size based on the percentage done. Its the only public variable I use.

Good eyes so far...;)

PS. Even though it doesnt apply to this problem, here is the code for the MyMsgBox function if anyone is curious.
 
Last edited:
Upvote 0
Hmm. I guess it's probably something with that myMsgBox function. Maybe you've got a reference to the called procedure (myMsgBox) and it's class object that won't let your calling class lose scope until it's released. Not sure. Seems like there's a lot going on.
 
Upvote 0
I dont think so. I wasn't using mymsgbox at the time I was getting crashes. I was using the built in msgbox.

Also, I never got any errors, excel would just crash when it hit the exit sub line. It didnt matter where I put the msgbox, it could be 20 lines up (i tried), and it would run through that no problem, execute all the code afterwards no problem, then it would crash at exit sub.

As soon as I removed the msgbox, and called it from a different procedure, everything was good.

I'm quite happy that it all works, just very baffled by it all and not sure what the original problem was...
 
Upvote 0
I see. I'm baffled also - it's a bit mysterious. For testing purposes I would remove or comment all the error handling in the class. It is possible that that is hiding some problem. Then set the editor to break in class modules. But probably at this point you may not care so much since its working!
 
Upvote 0
I have error handling disabled for debug...
Code:
If GblnErr Then On Error GoTo err Else On err GoTo 0
I set to false if I want error handling off....also that means I lied earlier, because the oBar variable is not my only public variable....this is as well. Sorry about that. Only two though, i swear! ;)

Anyways, yeah when I was debugging I had error handling off, and I had break in class module on when I was debugging the class....I also tried break on all errors (as opposed to unhandled errors) but wasn't getting anything unexpected (just stuff if the file or directory already existed, which you can see how thats handled in my code)...its a bit odd, right?

Hey, at least I'm not the only one who can't figure out the problem! Thats a little comforting...
 
Upvote 0
To be honest, this has happened to me on a few occasions over the years. But always I was able to fix the problem by rebooting! So it's been a very short-lived "problem" in my experience. Very hard to understand - you can't really blame the exit sub statement itself as it's pretty straightforward. If it continues to work as it is supposed to then I guess that's all that matters - it's not much of comfort, but as you say, I hope some comfort.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,289
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