Consulting

Results 1 to 4 of 4

Thread: Solved: Check if sheet name exists before deletion

  1. #1

    Solved: Check if sheet name exists before deletion

    I have searched and tried some code but I must be doing something stupid...

    I want part of my macro to delete a sheet (only if it exists).. if it does exist delete it and carry on with rest of code, if it doesn't exist, simply carry on with the rest of the code.

    The workbook has only one module in it... the sheet name it needs to check for and delete is "Adjusted Data"... the reason I need to delete it if its there is that the code afterwards copies another sheet's data and renames that to "Adjusted Data"..it will fail if the sheet name already exists.

    Also, any code you kindly provide...let me know if I need a seperate module or whatever as thats where I get stuck... I would prefer it if it is in one..

    NB the sub that copies and renames sheet is called "BlockedStockTidy"

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA] On Error Resume Next
    Sheets("Sheet3").Delete
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Also make sure that you reset the error handling after you are done.

    This will simply delete the sheet or ignore the error if there is no sheet.

    [VBA]
    On Error Resume Next
    Sheets("Sheet3").Delete
    On Error Goto 0
    [/VBA]

  4. #4
    cheers works a treat

Posting Permissions

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