+ Reply to Thread
Results 1 to 19 of 19

Create new worksheet by ticking checkbox and then delete worksheet by unchecking box

  1. #1
    Registered User
    Join Date
    10-19-2007
    Posts
    29

    Create new worksheet by ticking checkbox and then delete worksheet by unchecking box

    Hi, I have got someway with my limited VBA knowledge (it is probably ugly and bloated!) to making this work but am struggling on the delete aspect.

    I have a series of checkboxes which, if checked will create a new worksheet based on a hidden template within the workbook. If the user subsequently decides they don't need the worksheet, I want a message box to appear and for the worksheet to be deleted if the user selects Yes or Left if they select No and for the checkbox to be automatically populated again.

    I have posted where I have got to below and would appreciate any help given.

    Please Login or Register  to view this content.
    Ta
    Last edited by barefaced66; 01-19-2011 at 09:46 AM. Reason: Solved

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Create new worksheet by ticking checkbox and then delete worksheet by unchecking

    maybe something like this
    Please Login or Register  to view this content.
    Not sure why you have code to make the new sheet visible, is the template hidden?
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Registered User
    Join Date
    10-19-2007
    Posts
    29

    Re: Create new worksheet by ticking checkbox and then delete worksheet by unchecking

    RoyUK,

    That works great but what how would I need to amend the code if I wanted to generate a message box, if the box is then unchecked, asking the user whether they want the sheet deleted or not. If the user selects No, then the value of the checkbox needs to be set to True again.

    Thanks

  4. #4
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Create new worksheet by ticking checkbox and then delete worksheet by unchecking

    or
    Please Login or Register  to view this content.



  5. #5
    Registered User
    Join Date
    10-19-2007
    Posts
    29

    Re: Create new worksheet by ticking checkbox and then delete worksheet by unchecking

    snb, your code works fine with the line below commented out, as this gives a Run-time error 1004 (Method 'Range' of object'_Worksheet' failed)

    Please Login or Register  to view this content.
    Also, if you uncheck the checkbox and click the No button on the message box, then you get another run-time 1004 error, because the code is trying to create another worksheet named "BROCS" but the original one is still there (because I selected not to delete).

    Any other ideas?
    Thanks

  6. #6
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Create new worksheet by ticking checkbox and then delete worksheet by unchecking

    Can you explain what you are actually trying to do

  7. #7
    Registered User
    Join Date
    10-19-2007
    Posts
    29

    Re: Create new worksheet by ticking checkbox and then delete worksheet by unchecking

    RoyUK,

    The workbook has a top sheet which users will complete with various bits of infromation. The aim is to provide all of the relevant information that other users will require. However, occasionally the person completing the information may want to include a screen print from other systems that we have at work. To facilitate this, I have a series of check boxes named for the systems. If the person completing the spreadsheet checks a box, a worksheet will be created using a specific name (in this instance, BROCS). The user will then paste a screen print into the newly created worksheet. However, if the user then decides it is not required, I want them to be able to delete the worksheet by unchecking the box. I am also trying to account for someone incorrectly removing the check from the box, by asking whether they really want to delete the worksheet. If they don't then the code needs to repopulate the checkbox without this trying to create another worksheet with the same name.

    Hope this clarifies what I am trying to do

  8. #8
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Create new worksheet by ticking checkbox and then delete worksheet by unchecking

    To best describe or illustrate your problem you would be better off attaching a dummy workbook, the workbook should contain the same structure and some dummy data of the same type as the type you have in your real workbook - so, if a cell contains numbers & letters in this format abc-123 then that should be reflected in the dummy workbook.

    If needed supply a before and after sheet in the workbook so the person helping you can see what you are trying to achieve.

    Doing this will ensure you get the result you need!

  9. #9
    Registered User
    Join Date
    10-19-2007
    Posts
    29

    Re: Create new worksheet by ticking checkbox and then delete worksheet by unchecking

    OK, thanks for the advice Roy. I am attaching an example of what I am trying to achieve.

    You will see that checking the box will generate another worksheet based on the template worksheet (which would be hidden in the real life version). The user may then decide that he doesn't need the worksheet created and will uncheck the box. This will produce a message box asking if the user wants to delete the sheet. If they do, the code works and the sheet is deleted. If they change their mind, I want the sheet to stay and for the box to remain checked - at the moment my code doesn't do this and I imagine I need something to check and see whether the sheet is already present?

    Any suggestions?

    Many thanks
    Attached Files Attached Files

  10. #10
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Create new worksheet by ticking checkbox and then delete worksheet by unchecking

    Try this
    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    10-19-2007
    Posts
    29

    Re: Create new worksheet by ticking checkbox and then delete worksheet by unchecking

    Roy, thanks but, if the No option is selected on the message box, although the sheet is not deleted, neither is the checkbox value restored to TRUE - which is what I would like to happen to indicate that a worksheet with that name should be present

  12. #12
    Registered User
    Join Date
    10-19-2007
    Posts
    29

    Re: Create new worksheet by ticking checkbox and then delete worksheet by unchecking

    Is anyone able to give me an idea about the final piece of VBA that I need to recheck the box without the code looping back to the top and trying to create another worksheet with the same name?
    TIA

  13. #13
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Create new worksheet by ticking checkbox and then delete worksheet by unchecking

    If you restore the CheckBox to True then the code will trigger & create the sheet.

    The code that I posted works perfectly, removing the sheet if the user chooses Yes

  14. #14
    Registered User
    Join Date
    10-19-2007
    Posts
    29

    Re: Create new worksheet by ticking checkbox and then delete worksheet by unchecking

    RoyUk, yes I understand that and the code does work perfectly. Are you telling me that there is no way that I can reinstate the check box without triggering the code to create the worksheet again?

  15. #15
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Create new worksheet by ticking checkbox and then delete worksheet by unchecking

    What's the point. If the checkbox is checked I would expect that to mean that the sheet exists

  16. #16
    Registered User
    Join Date
    10-19-2007
    Posts
    29

    Re: Create new worksheet by ticking checkbox and then delete worksheet by unchecking

    RoyUK

    Yes, but if the user removes the check from the box and is presented with the message box and selects No (or Cancel) then the sheet remains in the workbook but the box is unchecked. If a user (and I am thinking about my lowest common denominator of user here) then checks the box again, an error will occur because the sheet is already present. I want to prevent that happening by making sure the checkbox value becomes TRUE again if the user selects to cancel the delete sheet action.

  17. #17
    Registered User
    Join Date
    10-19-2007
    Posts
    29

    Re: Create new worksheet by ticking checkbox and then delete worksheet by unchecking

    OK, I have been thinking and I wonder if I might be better implementing this via some sort of toggling button or something. Any views?

  18. #18
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Create new worksheet by ticking checkbox and then delete worksheet by unchecking

    Try this using a button
    Attached Files Attached Files

  19. #19
    Registered User
    Join Date
    10-19-2007
    Posts
    29

    Re: Create new worksheet by ticking checkbox and then delete worksheet by unchecking

    RoyUK, that works brilliantly. Many thanks for all of your time on this

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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