Consulting

Results 1 to 5 of 5

Thread: Solved: prevent worksheet deletion.

  1. #1

    Solved: prevent worksheet deletion.

    Hi.
    If i open wbk, code does not affect immediately?

    [VBA]Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    Dim ws As Worksheet

    Application.ScreenUpdating = False

    For Each ws In ThisWorkbook.Worksheets
    If ActiveSheet.Name = ("Data") Or ActiveSheet.Name = ("Query") Or ActiveSheet.Name = ("Sheet1") Then
    Outdel

    Else

    Indel

    End If
    Next ws
    Application.ScreenUpdating = True
    End Sub

    Sub Outdel()
    Dim Ctrl As Office.CommandBarControl
    For Each Ctrl In Application.CommandBars.FindControls(ID:=847)
    Ctrl.Enabled = False
    Next Ctrl
    End Sub

    Sub Indel()
    Dim Ctrl As Office.CommandBarControl
    For Each Ctrl In Application.CommandBars.FindControls(ID:=847)
    Ctrl.Enabled = True
    Next Ctrl
    End Sub
    [/VBA]

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Try Private Sub Workbook_Open() instead of Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    Thanks mdmackillop.
    Imho wbk open event triggered once (when wbk open) and depend of active Sheetname code enable or disabled Del option for all sheets.

  4. #4
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Hi there,

    Not super well tested, but an alternative would be to use the workbook activate and deactivate events. This way we haven't disabled the control for other workbooks that the user may have open in the same instance. Also, as you currently have it, you could close the wb (or even quit the app) with the control disabled.

    Again, not super well tested (running late to work...), but try:

    Option Explicit
        
    Private Sub Workbook_Activate()
        Call Workbook_SheetActivate(ActiveSheet)
    End Sub
        
    Private Sub Workbook_Deactivate()
        Call Indel
    End Sub
        
    Private Sub Workbook_SheetActivate(ByVal Sh As Object)
        Dim ws As Worksheet
         
        For Each ws In ThisWorkbook.Worksheets
            If ActiveSheet.Name = ("Data") _
            Or ActiveSheet.Name = ("Query") _
            Or ActiveSheet.Name = ("Sheet1") Then
                Outdel
            Else
                Indel
            End If
        Next ws
    End Sub
        
    Private Sub Outdel()
        Dim Ctrl As Office.CommandBarControl
        For Each Ctrl In Application.CommandBars.FindControls(ID:=847)
            Ctrl.Enabled = False
        Next Ctrl
    End Sub
        
    Private Sub Indel()
        Dim Ctrl As Office.CommandBarControl
        For Each Ctrl In Application.CommandBars.FindControls(ID:=847)
            Ctrl.Enabled = True
        Next Ctrl
    End Sub
    Hope that helps,

    Mark

  5. #5
    Big Thanks GTO.
    Works good.

Posting Permissions

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