+ Reply to Thread
Results 1 to 11 of 11

How to stop sub if Tab found with same name

  1. #1
    Registered User
    Join Date
    03-05-2020
    Location
    Stoke on Trent, England
    MS-Off Ver
    Excel 2016
    Posts
    59

    How to stop sub if Tab found with same name

    Hello all,

    I have tried a few bits of code now to exit a sub which creates and names a new sheet if a sheet with the name already exists.

    Ideally i'd like an info message to display alerting the user that their is an existing name and then for the operation to be aborted.

    However I keep getting the sheet being named (2) instead which is not the desired result. Please see code below and attached example sheet.

    If you click the button "Create focus list" then it will create a sheet from the template and name it based on whats in cell B6

    Any ideas?

    Please Login or Register  to view this content.

  2. #2
    Registered User
    Join Date
    03-05-2020
    Location
    Stoke on Trent, England
    MS-Off Ver
    Excel 2016
    Posts
    59

    Re: How to stop sub if Tab found with same name

    Example added
    Attached Files Attached Files

  3. #3
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,903

    Re: How to stop sub if Tab found with same name

    You could do something like this:

    Please Login or Register  to view this content.
    Rory

  4. #4
    Registered User
    Join Date
    03-05-2020
    Location
    Stoke on Trent, England
    MS-Off Ver
    Excel 2016
    Posts
    59

    Re: How to stop sub if Tab found with same name

    Thanks for this, It works great on the example sheet but when I put the code into my master sheet I have an issue because the next tab to performance hub isn't a generated sheet, its something else. So when I execute the macro it overwrites this sheet and gives an error. I can remedy it by moving the sheet to the left of the performance hub sheet but then the tab sequence doesn't flow right. How do I get it to miss the next tab after?

  5. #5
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,903

    Re: How to stop sub if Tab found with same name

    I don't follow. That code shouldn't care what order your sheets are in as it creates the new copy at the end of the workbook.

  6. #6
    Registered User
    Join Date
    03-05-2020
    Location
    Stoke on Trent, England
    MS-Off Ver
    Excel 2016
    Posts
    59

    Re: How to stop sub if Tab found with same name

    So if I change the :=Sheets(Sheets.Count) to ("The name of the last sheet")Then it works but what do I need to put if I want it to send it to the last sheet position?

  7. #7
    Registered User
    Join Date
    03-05-2020
    Location
    Stoke on Trent, England
    MS-Off Ver
    Excel 2016
    Posts
    59

    Re: How to stop sub if Tab found with same name

    Ah I see the issue. it doesn't like it if my template sheet is hidden.
    .
    Brings me to my next question.... Is it possible to hide sheets and still work with them?. Ideally I don't want the template sheet to be visible.

  8. #8
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,903

    Re: How to stop sub if Tab found with same name

    For most things, yes it is, but for copying sheets, you're better off unhiding, copying, then re-hiding.

  9. #9
    Registered User
    Join Date
    03-05-2020
    Location
    Stoke on Trent, England
    MS-Off Ver
    Excel 2016
    Posts
    59

    Re: How to stop sub if Tab found with same name

    Ok I've added code to make visible copy then make invisible which works fine,

    I have an issue when adding a message box to inform that sheet already exists, it either works for both scenarios are none.

    Could I get help on where I need to insert the msgbox code to make this work please.

    Please Login or Register  to view this content.
    Last edited by Kevchenk0; 04-08-2020 at 02:45 PM.

  10. #10
    Registered User
    Join Date
    03-05-2020
    Location
    Stoke on Trent, England
    MS-Off Ver
    Excel 2016
    Posts
    59

    Re: How to stop sub if Tab found with same name

    Sorted it. Dim'd an integer and then used goto function to take me to the end if the duplicate was found.

    Learning slowly but surely

    Please Login or Register  to view this content.
    Last edited by Kevchenk0; 04-08-2020 at 03:53 PM. Reason: including code for resolution

  11. #11
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,903

    Re: How to stop sub if Tab found with same name

    You should avoid Goto statements if possible. All you need there is something like:

    Please Login or Register  to view this content.

+ 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. VBA code to stop printing if an Error is found
    By CharterJP in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-14-2015, 09:44 AM
  2. How to sum if number but stop if text is found?
    By Hnewb in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 08-28-2013, 07:31 PM
  3. [SOLVED] How to Stop & Exit Sub if cell value > 1 was found
    By tuongtu3 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 02-20-2013, 01:41 AM
  4. [SOLVED] VBA to display message box if a value in a column is found then stop the macro
    By dantgk1 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-09-2013, 01:15 PM
  5. Stop searching for file once a match is found.
    By CJPHX in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-21-2010, 12:39 PM
  6. How to Stop Error and Debug if filename is not found
    By pmarsella in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-19-2009, 05:59 AM
  7. [SOLVED] Stop the loop when found.
    By hfazal in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-16-2006, 08:10 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