+ Reply to Thread
Results 1 to 8 of 8

Check if command button is on worksheet, delete worksheet if not

  1. #1
    Registered User
    Join Date
    07-24-2015
    Location
    Toledo, OH
    MS-Off Ver
    Microsoft Office 2010
    Posts
    85

    Check if command button is on worksheet, delete worksheet if not

    Hi all,

    I'm trying to figure out how to get rid of any extra worksheets my users might add to a workbook by accident. They are not supposed to add any worksheets themselves at all. I can't protect the workbook structure because there are macros that programmatically and legitimately add new worksheets to the database. However, one thing that happens is a button called "SuperAdminButton" is created on every new legitimate sheet, which is something a user wouldn't do themselves. The button is used for navigation.

    SO. I need a macro that will be included in my BeforeClose event that will check each worksheet in the workbook, look to see if there's a command button on there with the name "SuperAdminButton," and if there is no button like that, delete the sheet without warning.

    The button is a form control, if that matters.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Check if command button is on worksheet, delete worksheet if not

    Couldn't you use the workbook level event NewSheet to delete any unauthorized sheets that are added?

    That event is triggered whenever a new sheet is added to the workbook and you could have code in it to check if the new sheet is legitimate or not.
    If posting code please use code tags, see here.

  3. #3
    Valued Forum Contributor Gatti's Avatar
    Join Date
    06-08-2015
    Location
    Brasil, São Paulo, Ribeirão Preto
    MS-Off Ver
    365
    Posts
    346

    Re: Check if command button is on worksheet, delete worksheet if not

    Hello

    Try this (not very smart code... but works)

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    07-24-2015
    Location
    Toledo, OH
    MS-Off Ver
    Microsoft Office 2010
    Posts
    85

    Re: Check if command button is on worksheet, delete worksheet if not

    Norie, how would I code that, exactly? I'm happy to use that event, and considered it at the beginning of this conundrum, but I need to know how to make VBA realize whether a worksheet is legitimate or not.

  5. #5
    Valued Forum Contributor Gatti's Avatar
    Join Date
    06-08-2015
    Location
    Brasil, São Paulo, Ribeirão Preto
    MS-Off Ver
    365
    Posts
    346

    Re: Check if command button is on worksheet, delete worksheet if not

    Jerbinator, my code didn't work?

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Check if command button is on worksheet, delete worksheet if not

    @Gatti

    Here is another way using the Forms Buttons collection.

    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  7. #7
    Valued Forum Contributor Gatti's Avatar
    Join Date
    06-08-2015
    Location
    Brasil, São Paulo, Ribeirão Preto
    MS-Off Ver
    365
    Posts
    346

    Re: Check if command button is on worksheet, delete worksheet if not

    @Leith

    Well... much better and optmized than mine
    Thanks for the feedback!

  8. #8
    Registered User
    Join Date
    07-24-2015
    Location
    Toledo, OH
    MS-Off Ver
    Microsoft Office 2010
    Posts
    85

    Re: Check if command button is on worksheet, delete worksheet if not

    I ended up using a mixture of these ideas. @Leith, when I ran your code, VBA did not recognize the "Excel.Button" dimension and started deleting sheets left and right. (Amen for backup copies, right?) So I defined it as a shape, the way @Gatti did, and it recognized it after that. I just ran a test where I added a sheet and put a form button on it, but didn't name it the same way, and the code deleted that sheet and left the rest. Perfect! Thank you all!

    I have a ton of things going on already in the BeforeClose event, but here's the snippet of code I ended up using for this:

    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 create command button. Print all sheets then delete command button.
    By Declamatory in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-15-2015, 05:18 PM
  2. command button to take you to another cell in same worksheet
    By marcbarnett in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-21-2014, 10:54 AM
  3. Need help on the Command Button on the destination worksheet
    By Catine in forum Excel Programming / VBA / Macros
    Replies: 19
    Last Post: 04-02-2013, 05:33 PM
  4. [SOLVED] Can't delete Command Button on Worksheet
    By eemiller1997 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-07-2013, 02:20 PM
  5. [SOLVED] Delete button on another worksheet, WITHOUT selecting worksheet?
    By Rerock in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-04-2012, 12:40 PM
  6. Command Button on worksheet
    By Jack in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-12-2006, 06:55 AM
  7. [SOLVED] command button on worksheet
    By PhilC in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-19-2006, 12:15 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