(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:
So it was here and not in the actual .Copy command?

HP printers have always been a pain in Excel VBA. Usually they just kill performance, and don't crash and burn.

Don't hold your breath waiting for any kind of hotfix.
 
Upvote 0

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Microsoft acknowledges the problem (last review 8/6/11):
http://support.microsoft.com/kb/2555016/

But I can't tell if either Microsoft or HP intend to fix this incompatibility.
I will be emailing my users to ask who has an HP printer and ask them to notify me if they have any problems with Excel crashing or giving odd error messages.

I'm relieved that turning off Application.PrintCommunication while doing Page Setup commands in macros appears to have fixed my problem AND makes my macro run much faster.

Watch out: Macros that re-set Left/Center/Right headers (if previously set on the Workbook) may fail. There's a bug related to turning off PrintCommunication prior to setting multiple header/footer sections:

http://www.edugeek.net/blogs/pico/755-excel-2010-printer-fails-communicate.html
 
Upvote 0
When I said, "don't hold your breath," I was implying that nobody is intending to fix anything. This is the kind of issue that persists for versions of software. It might become part of a service pack, or it might geer fixed in the next main version of Excel or Windows. Maybe.
 
Upvote 0
I think I've determined that I can forego the crash if I don't set Application.DisplayAlerts = False. When I do it by hand, the program never crashes.

Is there a quick/easy way to add a few lines of code that will simply answer "Delete" to the "Data may exist in the sheet(s) selected for deletion. To permanently delete the data, press Delete" dialog box without using Application.DisplayAlerts?
 
Upvote 0
One other thing that I'm not sure is related to the crashes. When I run my CreateWorkbooks code and step through it, I noticed some very odd behavior (at least behavior that I'd never have expected). The following two functions execute multiple times for no apparent reason when I step-through the code.

Code:
Function ShowCacheIndex(rngPT As Range) As Long
  ShowCacheIndex = rngPT.PivotTable.CacheIndex
End Function

Function ShowSource(rngPT As Range) As String
  ShowSource = rngPT.PivotTable.SourceData
End Function

I had forgotten about this because I had commented them out of the code, but when I wanted to test the pivotcache and source to make sure my four pivot tables were all coming/sourcing from the same place, I uncommented them and noticed this again.
 
Upvote 0
...The following two functions execute multiple times for no apparent reason when I step-through the code.
...

When this happens to me its usually because the event that triggers these functions occurs more than once -- even though it didn't seem to me that it would. Set a breakpoint on that function or step through and see where it's running each time.
 
Upvote 0
Is there a quick/easy way to add a few lines of code that will simply answer "Delete" to the "Data may exist in the sheet(s) selected for deletion. To permanently delete the data, press Delete" dialog box without using Application.DisplayAlerts?

If you're deleting a sheet:

Code:
SendKeys "{Enter}"
Sheets("Sheet3").Delete  'replace with your sheet name

Be prepared to be ridiculed for resorting to SendKeys if you post that code. ;)

When I had my crashing problem...all sorts of simple actions behaved incorrectly. So your problem with DisplayAlerts could be caused by a seemingly unrelated issue. Once you figure that out you can turn off DisplayAlerts again.
 
Upvote 0
...When I had my crashing problem...all sorts of simple actions behaved incorrectly...

I realize this is a very old thread, but I wanted to offer a solution to anyone else who may still be searching for this problem (Excel 2010 crashing, System Error &H800401F0, CoInitialize has not been called, etc.) The problem is with HP printers with some age on them, usually the Officejet line. The best fix for this is to open Excel 2010 and immediately change your printer to something else, THEN you can proceed without the fails.

The only fix HP has issued (at least that I have found so far and depending on your printer model) is to install a universal driver, but I have not had much luck with that yet. They will not fix this problem, short of you buying a new printer.

Good luck.
 
Upvote 0
I realize this is a very old thread, but I wanted to offer a solution to anyone else who may still be searching for this problem (Excel 2010 crashing, System Error &H800401F0, CoInitialize has not been called, etc.) The problem is with HP printers with some age on them, usually the Officejet line. The best fix for this is to open Excel 2010 and immediately change your printer to something else, THEN you can proceed without the fails.

The only fix HP has issued (at least that I have found so far and depending on your printer model) is to install a universal driver, but I have not had much luck with that yet. They will not fix this problem, short of you buying a new printer.

Good luck.

That's so interesting! How on earth did you determine this was the issue? That's pretty incredible. I'll have to resurrect my old non-working code and give this a try. The printer at this client is definitely set to an HP by default.

I'm so, so curious how you were able to diagnose this! Thanks for posting it here.
 
Upvote 0
That's so interesting! How on earth did you determine this was the issue? That's pretty incredible. I'll have to resurrect my old non-working code and give this a try. The printer at this client is definitely set to an HP by default.

I'm so, so curious how you were able to diagnose this! Thanks for posting it here.

I did a LOT of searching, and finally came across this Microsoft support bulletin: http://support.microsoft.com/kb/2555016

One thing to mention, make sure that you change printers first (before you proceed) or you will continue getting the crashes.

Kind of aggravating that neither will correct the issue, but I guess it's not Microsoft's problem, and HP just wants to sell another printer.
 
Upvote 0

Forum statistics

Threads
1,224,567
Messages
6,179,568
Members
452,926
Latest member
rows and columns

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