+ Reply to Thread
Results 1 to 11 of 11

Excel Crashes when Sheet is Deleted

  1. #1
    Forum Contributor
    Join Date
    04-13-2010
    Location
    England
    MS-Off Ver
    Office 2016 365
    Posts
    211

    Excel Crashes when Sheet is Deleted

    I posted about this a while ago and i thought i'd worked around the issue but i haven't.

    Wasn't sure whether i was meant to revive the old thread or start a new one, sorry if i've done the wrong thing!

    This time i'm attaching a workbook - i've sanitised and simplified it.

    There is a control button on the "Instructions" tab that calls the macro delete_sheets. It should delete each sheet after the user defined administrative sheets.

    I have a couple of UDFs which calculate on the "Instructions" tab but the code is in their own module. The UDFs use the sheets.count calculation.

    When i click the "Delete All Scripts" button (in the attached i would expect it to delete two sheets BP 2 and BP 3). Excel Crashes completely.

    I'm sure the conflict is between having a UDF that counts sheets and command that deletes them but i'm stumped as to what a work around is. Any takers?

    Thanks,

    J
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Excel Crashes when Sheet is Deleted

    You have a very complicated code for simply deleting sheet.
    This line causes excel to crash.
    Sheets(Strtsht + 1).Delete
    I am not a big fun of do/while loop and there is not need for this task.
    Just loop through the sheets and delete them.

  3. #3
    Forum Contributor
    Join Date
    04-13-2010
    Location
    England
    MS-Off Ver
    Office 2016 365
    Posts
    211

    Re: Excel Crashes when Sheet is Deleted

    I did that so the sheet after the admin sheet (strhsht) gets deleted each time -

    I'm trying to say delete sheets 4 thru x

    By deleting sheet 4 until the loop reaches x i'm always deleting an existing sheet. If i looped up through index numbers eventually it would try to delete a sheet that didn't exist....

  4. #4
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Excel Crashes when Sheet is Deleted

    No,
    You can either exclude some sheets from the loop (For-Next) or use an array to store the names of sheet you want to delete.

  5. #5
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Excel Crashes when Sheet is Deleted

    You need error handling in your functions:
    Please Login or Register  to view this content.
    for example. You can also simplify the deletion code:
    Please Login or Register  to view this content.
    Remember what the dormouse said
    Feed your head

  6. #6
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Excel Crashes when Sheet is Deleted

    Rory,
    Good man, you have not used do/while crap.

  7. #7
    Forum Contributor
    Join Date
    04-13-2010
    Location
    England
    MS-Off Ver
    Office 2016 365
    Posts
    211

    Re: Excel Crashes when Sheet is Deleted

    Thank you both!
    AB33 - i went away and changed the loop to a "For Next" with some exclusion logic and it didn't crash on the Delete command!
    RomperStomper - Thanks, i'll definitely add the error handling to the functions

    Thank you both for your help!

  8. #8
    Forum Contributor
    Join Date
    04-13-2010
    Location
    England
    MS-Off Ver
    Office 2016 365
    Posts
    211

    Re: Excel Crashes when Sheet is Deleted

    As a side note, why is Do while or Do Until so bad?

  9. #9
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Excel Crashes when Sheet is Deleted

    Because they can lead you to infinite loop. This can cause your beloved PC to crash.

  10. #10
    Forum Contributor
    Join Date
    04-13-2010
    Location
    England
    MS-Off Ver
    Office 2016 365
    Posts
    211

    Re: Excel Crashes when Sheet is Deleted

    OK, makes sense. Noted for future reference!

    Thanks!

  11. #11
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Excel Crashes when Sheet is Deleted

    There's nothing wrong with Do loops per se (aside from the risk of endless loops) and they do have their uses, but when you know a start and end point it makes sense to use a For... Next structure instead.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 0
    Last Post: 08-28-2013, 06:45 AM
  2. EXCEL: Undo If Particular Sheet Deleted
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-28-2013, 01:19 PM
  3. Excel crashes everytime I copy sheet to a new workbook
    By este994 in forum Excel General
    Replies: 0
    Last Post: 03-06-2012, 12:42 PM
  4. Excel crashes when unhiding or hiding sheet
    By Lifesigns in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-26-2010, 05:07 PM
  5. MS EXCEL SHEET CRASHES WHEN VIEWING
    By Wally in forum Excel General
    Replies: 1
    Last Post: 09-07-2005, 03:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1