Consulting

Results 1 to 6 of 6

Thread: Protect sheet from being deleted

  1. #1
    VBAX Regular
    Joined
    Nov 2005
    Posts
    30
    Location

    Protect sheet from being deleted

    Hi,
    I'm looking for a way to protect certain sheets from being deleted. They may be modified by VBA or by the user, but never deleted.

    Due to the amount of codes and subs of the actual workbook, the "traditional" protect/unprotect procedure (for each macro) is not an alternative.

  2. #2
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
     
    Option Explicit
     
    Private Sub Worksheet_Activate()
          With Application
                .CommandBars("Worksheet Menu Bar").Controls("Edit").Controls("Delete Sheet").Enabled = False
                .CommandBars("Ply").Controls("Delete").Enabled = False
          End With
    End Sub
     
    Private Sub Worksheet_Deactivate()
          With Application
                .CommandBars("Worksheet Menu Bar").Controls("Edit").Controls("Delete Sheet").Enabled = True
                .CommandBars("Ply").Controls("Delete").Enabled = True
          End With
    End Sub
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  3. #3
    VBAX Regular
    Joined
    Nov 2005
    Posts
    30
    Location
    Now that's a truly brilliant solution! Many thanks. However, I assume the Controls("Edit") are defined according to a English version of Excel? I'm getting an error message (my translation):

    Run Error nr '5'.:
    Procedure call or argument not valid.
    Any ideas? I'm not sure how to manually disable a control while recording a macro... The "Delete Sheet" msoControlButton has "ID:=847", that's all I seem to get while recording.

  4. #4
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Yes, it's an english version, but just use the text that's wriiten on another language version - OR - try the number as: .Controls(847).Enabled = False
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  5. #5
    VBAX Tutor gnod's Avatar
    Joined
    Apr 2006
    Posts
    257
    Location

    Question

    Hi,

    when i right click the Sheet1 the delete command is disabled and that is the purpose of the code but when i right click Sheet2 the delete command is also disabled but i can delete the sheet

    pls refer to the attached file..


    thanks..

  6. #6
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by gnod
    Hi,

    when i right click the Sheet1 the delete command is disabled and that is the purpose of the code but when i right click Sheet2 the delete command is also disabled but i can delete the sheet

    pls refer to the attached file..


    thanks..
    It only shows as being disabled because you used the right-click to activate sheet2 - but it's actually not (and shouldn't be) so you can delete it. If you activate sheet2 with a left-click this doesn't happen
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

Posting Permissions

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