Preventing copy/move sheet INTO main workbook from outside workbook

PaulZak

Board Regular
Joined
Aug 22, 2006
Messages
105
I'm using this Workbook's SaveSheet event to prevent users from adding a new sheet from within my main workbook file. OK, that's half the goal.

But that event isn't triggered if user tries to copy/move a sheet from another WB into my main file.

I've started looking at a workbook lock/protect setting but we have numerous procedures that add temp sheets in workbook for data sheets, charts, etc. So, if I do something like lock or protect the workbook, we have to find each such instance when sheets are added, changed, deleted and possible add code to unlock/unprotect, add sheet, relock/protect each time. This isn't ideal.

Any suggestions for how to prevent users from copying/moving a sheet into the main workbook?
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
If you can't protect the workbook then you may want to try the following vba workaround which should prevent the user from copying/moving sheet(s) coming from other workbooks into the main workbook but still allow sheets to be copied/moved from within the main workbook.


Code goes in the ThisWorkbook Module of the main workbook:
Code:
Option Explicit

Private bSheetCopiedOrMovedFromOtherWorkbook As Boolean
Private oLastActiveSheet As Worksheet


Private Sub Workbook_Activate()
    bSheetCopiedOrMovedFromOtherWorkbook = True
End Sub

Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
    Set oLastActiveSheet = Sh
End Sub

Private Sub Workbook_SheetActivate(ByVal Sh As Object)

    On Error GoTo errHandler
    
    With Application
        If bSheetCopiedOrMovedFromOtherWorkbook Then
            bSheetCopiedOrMovedFromOtherWorkbook = False
            .EnableEvents = False
            .ScreenUpdating = False
            .DisplayAlerts = False
            Sh.Delete
            If Not oLastActiveSheet Is Nothing Then
                oLastActiveSheet.Activate
            End If
            MsgBox "Operation not allowed"  [COLOR=#008000][B]'<=== You Can Remove This Optional MsgBox.[/B][/COLOR]
        End If
        
errHandler:
        .EnableEvents = True
        .ScreenUpdating = True
        .DisplayAlerts = True
    End With
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,563
Messages
6,120,248
Members
448,952
Latest member
kjurney

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top