+ Reply to Thread
Results 1 to 7 of 7

If sheet exists called Sheet* then delete

  1. #1
    Valued Forum Contributor
    Join Date
    08-10-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    721

    If sheet exists called Sheet* then delete

    I have a workbook with many sheets labelled properly, but occassionally my colleagues add a new sheet to the spreadsheet and save it which knackers my formulas.
    I want to add a check in my beforeclose function that checks for sheets that begin with the word Sheet, so anything called Sheet1, Sheet2 etc is to be deleted.

    What i have so far is:

    Please Login or Register  to view this content.
    Whilst this works great if there is just the Sheet1, it does not work for Sheet2 onwards as I cant quite figure out how to loop the code.

  2. #2
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: If sheet exists called Sheet* then delete

    Something like:
    Please Login or Register  to view this content.
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  3. #3
    Valued Forum Contributor
    Join Date
    08-10-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    721

    Re: If sheet exists called Sheet* then delete

    Wow that was quick!
    it works brilliantly, if I may though - can you edit the code to go to the sheet its about to delete, so the user can see if there is any data on there worth keeping?
    ws.name.select doesnt work...

    nevermind figured it out...it needed

    Worksheets(ws.Name).Activate

    Solved...rep added.
    Last edited by nickmax1; 07-06-2016 at 06:57 AM.

  4. #4
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: If sheet exists called Sheet* then delete

    Add the line highlighted red:
    Please Login or Register  to view this content.

  5. #5
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: If sheet exists called Sheet* then delete

    Maybe:

    Please Login or Register  to view this content.

  6. #6
    Valued Forum Contributor
    Join Date
    08-10-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    721

    Re: If sheet exists called Sheet* then delete

    John, thanks very much - that code works even faster still!

    rep added

  7. #7
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: If sheet exists called Sheet* then delete

    You're welcome. Glad to help out. Thanks for the feedback and reps.

+ 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] Delete sheet if it exists
    By jomili in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 10-15-2015, 03:08 PM
  2. [SOLVED] Check range from one sheet against range in another sheet and then delete row if exists
    By zigojacko in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 12-19-2013, 12:37 PM
  3. [SOLVED] Check if sheet exists and delete it using IF ERROR function in VBA
    By Jardim in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-25-2012, 12:06 PM
  4. Replies: 0
    Last Post: 08-08-2012, 10:20 AM
  5. Delete a sheet, if it exists
    By LydiasDad in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-13-2011, 09:06 PM
  6. Macro to copy sheet from closed work book and overwrite if sheet already exists
    By munkee in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 11-23-2009, 06:36 PM
  7. delete sheet if exists help needed
    By mattfaust in forum Excel General
    Replies: 2
    Last Post: 08-30-2006, 12:28 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