+ Reply to Thread
Results 1 to 13 of 13

Protect specific sheets from deletion

Hybrid View

  1. #1
    Registered User
    Join Date
    12-17-2007
    Posts
    14

    Protect specific sheets from deletion

    Is there anyway to prevent certain sheets from be deleted?

    i know there is protect workbook, but that prevents the user from deleting any sheet

  2. #2
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    See if example helps

    Code used

    In sheet1 module (right click sheet1 rab > select view code)
    Private Sub Worksheet_Activate()
      Dim CB As CommandBar
      Dim Ctrl As CommandBarControl
      For Each CB In Application.CommandBars
        Set Ctrl = CB.FindControl(ID:=847, recursive:=True)
        If Not Ctrl Is Nothing Then
          Ctrl.OnAction = "PreventDelete"
          Ctrl.State = msoButtonUp
        End If
      Next
    End Sub
    Private Sub Worksheet_Deactivate()
      Dim CB As CommandBar
      Dim Ctrl As CommandBarControl
      For Each CB In Application.CommandBars
        Set Ctrl = CB.FindControl(ID:=847, recursive:=True)
        If Not Ctrl Is Nothing Then Ctrl.OnAction = ""
      Next
    End Sub
    Code in a normal module
    Public Sub PreventDelete()
    
        MsgBox "Sorry, Cannot delete this sheet!", _
        Buttons:=vbExclamation, _
        Title:="Cannot Delete Sheet!"
    End Sub
    VBA Noob
    Attached Files Attached Files
    Last edited by VBA Noob; 12-19-2007 at 04:14 PM.
    _________________________________________


    Credo Elvem ipsum etian vivere
    _________________________________________
    A message for cross posters

    Please remember to wrap code.

    Forum Rules

    Please add to your signature if you found this link helpful. Excel links !!!

  3. #3
    Registered User
    Join Date
    12-17-2007
    Posts
    14

    wow

    awesome, thanks man!

  4. #4
    Registered User
    Join Date
    12-17-2007
    Posts
    14
    so uh how do i turn it off? lol
    i ran it on a sheet i do want to be able to delete and can't figure it out ha

  5. #5
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    Code is triggered when you select or unselected sheet1. Unselect sheet1 to turn off

    VBA Noob

  6. #6
    Registered User
    Join Date
    12-17-2007
    Posts
    14
    so uh, i tried making a new workbook entirely, and the macro followed it there

    for example, lets say i select sheet3, and then right click to try and delete, it pops open the excel with the preventdelete macro and doesn't allow me to do even if i disable macros

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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