(Excel 2010) VBA worksheet copy = Crash (but stepping through does NOT!)

analyst44

Board Regular
Joined
May 19, 2004
Messages
127
Hi Everyone,

I've tried to troubleshoot this for two days now and I can't make heads or tales of it. I have a bunch of subroutines in Excel 2010 that I'm using to manipulate pivot tables, grab values, make reports and then export them out of the "engine" workbook into their own Excel 97 worksheets.

Everything is working great except for when I either call the CreateWorkbooks subroutine from the main code or when I try to execute it by just clicking on the macro button. It blows up my entire workbook, crashes and leaves me with no recourse to figure out why it's happening. From the little I can gather, it does appear to be happening at the step where the individual worksheet is copying, though I'm not 100% certain about it.

Here's the code with what I believe to be the offending line highlighted in red:

Rich (BB code):
Sub CreateWorkbooks_v3()
    'Creates an individual workbook for each worksheet in the active workbook.
    Dim wbDest As Workbook
    Dim wbSource As Workbook
    Dim sht As Worksheet
    Dim strSavePath As String
    Dim FileExtStr As String
    Dim FileFormatNum As Long
    Dim strSavePathFull As String
 
    strSavePath = Left(ThisWorkbook.Path, InStrRev(ThisWorkbook.Path, "\") - 1)  'Change this to suit your needs
    Set wbSource = ActiveWorkbook
    Application.DisplayAlerts = False
 
 
    For Each sht In wbSource.Sheets
 
        Select Case sht.Name
            Case "WBK_Info", "VBA_Values", "Role_Picks", "PVT_Play", "Custom_Report", "Master_Pivot", "Template"
 
            Case Else
                sht.Copy
                Set wbDest = ActiveWorkbook
 
                With wbDest
                    If wbSource.Name = .Name Then
                        MsgBox "Your answer is NO in the security dialog"
            '            GoTo GoToNextSheet
                    End If
 
                    strSavePathFull = strSavePath & "\" & sht.Name & ".xls"
                    wbDest.SaveAs Filename:=strSavePathFull, FileFormat:=56
                    wbDest.Close 'Remove this if you don't want each book closed after saving.
 
                 End With
 
                sht.Delete
        End Select
 
    Next
 
    Application.DisplayAlerts = True
    Application.Calculation = xlCalculationAutomatic
 
 
 
 
End Sub

A few notes:

  1. I've used Select/Case and If/Then Statements to code for the exceptions. Neither have had an impact.
  2. When I take out both of those and just cycle through every worksheet in the workbook with no exceptions, I do not get the error.
  3. The error I am getting is not consistent: If I step through the code using F8, it almost never crashes. I've also had the code work successfully from a SubRoutine call from within my code and from just hitting the the "Select Macro" from the Developer Tab. But, both of those are when the program usually crashes and Excel Exits. That scenario fails sometimes if there is just one worksheet that is not an exception or if there are many. Also, the even weirder thing is that when there are many worksheets to copy, it will sometimes execute successfully for the first few and then it'll crash on like iteration 4 or 5.
#3 is my biggest frustration. The error is not consistent, not easy to troubleshoot and not something I know how to handle when it's happening sometimes but not in others. I'm assuming there may be something in my template (that is creating the sheets to be copied) that is causing the issue, but I can't imagine what it would be. Each worksheet that is ultimately copied only has pasted values and a chart at the bottom. There are no forumulas or anything in the worksheets to be exported.

Any help on this would be awesome. Thanks so much for taking the time to read my post.
 
Last edited:

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
When code works fine using F8 but crashes when run at speed, it usually means Excel and Windows get out of sync. You can insert one or more DoEvents commands in the code, to tell Excel to wait while Windows does what it is trying to do. Something like:

Code:
    DoEvents
    sht.Copy
    DoEvents
 
Upvote 0
Oh wow. THE Jon Peltier. I'm honored! I've read so much about you and run into both your replies on sites like this and your own pages over the years more times than I can even count. Really admire you and the rest of the Excel MVPs -- I'm currently enrolled in the Excel Hero Academy to try to take my skills to the next level (:

The funny thing about what you said above is that, even though it was completely counter-intuitive to me that something could work with F8 and only sometimes when run, I had a feeling that it was "going too fast" and tried to put in an Application.Wait command for a few seconds. For smaller durations (1-3s) the code was still volatile -- crashing sometimes and not crashing in others. For longer durations (5+), this seemed to stabilize it a bit, if I recall correctly. Don't quote me on that -- I tried it for a bit and then abandoned it because I was so mad that the solution made no sense!

I will research the DoEvents command tonight or tomorrow and get caught up on that. This mention of it is my first exposure. I'll report back either way.

Thank you for taking the time, Jon!
 
Upvote 0
My macros that have been working for years in Excel 2003 (and after a recent upgrade to 2010) -- are suddenly crashing this week in Excel 2010.

It started with another user. Then last night it began happening to me too. Our corporate IT pushes updates to our laptops so this may be a symptom of a security update of some kind.

I can step through macros without a crash. Running macro normally crashes Excel. When I put in breakpoints I find that the crash does not occur at the same place each time. This should be the kind of behavior that does not cost *me* $259 to report to the vendor. Grrr..r..r..:mad:

If I find any solution I'll report back. Thanks in advance if you do to!

Sister D.
 
Upvote 0
I'm triangulating with DoEvents here and there to see if there's a source.
While copying the command "DoEvents" so I could paste elsewhere in the code, I got a MS Visual Basic error "Out of Memory" while editing the code. Pops up when I try to Copy again.

Despite these unusual errors I gave the macro a go and it stopped with an error (with nothing changed in the code except the DoEvents) that said:
"run-time error 3320, in table-level validation expression." Debug took me to a recordset.Edit command it didn't like. This macro has been running fine until yesterday.

I may need an exorcism.
I'm rebooting. Back in a moment...
 
Upvote 0
Further info: I was chasing down the line of code that seemed to be causing the error. But after I put some breakpoints in at earlier spots in the code, it almost seems as if the crash is already beginning as the code is still running. So my macro runs successfully to the breakpoint and I can see the highlighted line on the breakpoint...but then the crash occurs. When no command is being run. And when I move the breakpoint up earlier in the macro, the crash still occurs although the code below the breakpoint just ran minutes ago.

It seems the crash is already on collision course before my code finishes. Some of the code will continue to run successfully before the crash finally kills Excel. So trying to find the offending line of code based on the time of the crash isn't getting me anywhere.

I am accessing data from a legacy Access database (.mdb) using ADO.
A likely source of trouble? I'll keep at this...
 
Upvote 0
Ok, a few observations. With the DoEvents wrapped around Sht.Copy, the code executed successfully when I ran that subroutine independently. However, if I called it using "Call CreateWorkbooks_v3" at the end of another master routine (preferred method), it crashed. Stumped, I realized that I wasn't *sure* if the Sht.Copy command was the culprit, so I removed the DoEvents from there and decided to add the following to the Master Routine:

Code:
DoEvents
Call CreateWorkbooks_v3
DoEvents

So far, that seems to do the trick and I've done two different kinds of tests runs in which the code has run successfully without interruption. I still have a few more things I need to tweak and try, but I'm certainly much more hopeful than I was! Thanks so much, Jon! :)


In other news, I found this thread about Application.Wait versus DoEvents. I'm guessing that the former actually halts code execution while *not* removing the "focus" from said execution/run whereas the latter actually allows for background processes to be accomplished rather than VBA/Excel dominating the computer's *something* (CPU, processing, I'm not sure).

Is that even close to right?
 
Upvote 0
Sometimes I've had to sprinkle DoEvents liberally throughout a procedure.

My understanding of DoEvents vs Application.Wait matches what that thread states. Application.Wait simply freezes up the machine while Excel waits.
 
Upvote 0
Update: I cannot execute my code successfully anymore. Stepping through with F8, it's happening at Sht.copy line. The only thing I've changed is code in other sub routines that run before this.

There are DoEvents wrapped around that statement, as well as the entire subroutine as noted above.

It's now also triggering a crash if I comment out the call from the Master Sub and run the subroutine (CreateWorkbooks_v3()) separately.

Totally stumped.
 
Upvote 0

Forum statistics

Threads
1,224,564
Messages
6,179,547
Members
452,925
Latest member
duyvmex

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