+ Reply to Thread
Results 1 to 3 of 3

VBA execution exits after call to Worksheet.Delete. No error, no debug prompt, just quits

  1. #1
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,705

    VBA execution exits after call to Worksheet.Delete. No error, no debug prompt, just quits

    The attached file is a tool to create weekly project reports from an external data source. (I have deleted everything except what is needed to demonstrate the problem.) Some sheets are temporary and are deleted and re-created each time the tool is run. These temporary sheets have tabs with no color or black color. Sheets having tabs with other colors are part of the baseline tool and should never be deleted.

    There is a button on the Control sheet to invoke a sub that deletes all of the temporary sheets. The button executes sub ResetSheets in module MainCommon. This Sub has a loop to check every sheet, and if it is not a baseline sheet, it deletes it.

    The module Exclude has a function StaticSheet that takes a sheet name and returns a Boolean TRUE iff the sheet is a baseline sheet and should not be deleted.

    The problem is that when this code finds a sheet to delete, it successfully deletes it, but then execution stops after the first deletion. There is no error, no debug prompt--it just stops execution as if it were a normal exit. If I repeat execution, it will move on to delete the next sheet in line, but then once again stops with no error. When I step through the code in the debugger, the behavior is the same: the Delete line is executed then execution just stops. It does not give an error nor move on to the next line of code.
    Please Login or Register  to view this content.
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  2. #2
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: VBA execution exits after call to Worksheet.Delete. No error, no debug prompt, just qu

    Deleting items from a collection you are iterating through isn’t a good idea. I’d read the sheet names into an array/collection of strings and then loop through that to delete the sheets

  3. #3
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,705

    Re: VBA execution exits after call to Worksheet.Delete. No error, no debug prompt, just qu

    I'll try your suggestion, but
    Quote Originally Posted by Kyle123 View Post
    Deleting items from a collection you are iterating through isn’t a good idea.
    why? If it causes an invalid condition (e.g., the For loop tries to advance to the next object in the collection but I've already deleted it), it should result in an error or bug, but I would not expect it to cause a halt to execution in the middle of a sub.

+ 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. [SOLVED] Error handling with Call Stack and Execution Trace
    By Warbe in forum Tips and Tutorials
    Replies: 5
    Last Post: 01-01-2020, 06:36 AM
  2. Replies: 1
    Last Post: 10-12-2019, 09:02 AM
  3. Replies: 3
    Last Post: 11-19-2015, 03:08 PM
  4. Using Call Module1.Offset = debug error
    By garretonufer in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 09-29-2009, 05:12 PM
  5. Save Excel Worksheet - Debug Error
    By BrownTeddyBear in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-26-2009, 04:15 AM
  6. Delete worksheet with no user prompt with VBA
    By Graham Whitehead in forum Excel General
    Replies: 2
    Last Post: 06-21-2006, 12:25 PM
  7. remove Worksheet using Sh.Delete without prompt
    By coco in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-21-2005, 06:05 PM

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