Consulting

Results 1 to 10 of 10

Thread: Prevent Copying Worksheets

  1. #1

    Prevent Copying Worksheets

    Hi,

    In a protected workbook, how do I stop the copying of a sheet(s) to a new workbook, please ?

    Thanks.

  2. #2
    VBAX Expert Logit's Avatar
    Joined
    Sep 2016
    Posts
    613
    Location
    '*** In a standard module ***

     
    Sub ToggleCutCopyAndPaste(Allow As Boolean)
         'Activate/deactivate cut, copy, paste and pastespecial menu items
        Call EnableMenuItem(21, Allow) ' cut
        Call EnableMenuItem(19, Allow) ' copy
        Call EnableMenuItem(22, Allow) ' paste
        Call EnableMenuItem(755, Allow) ' pastespecial
         
         'Activate/deactivate drag and drop ability
        Application.CellDragAndDrop = Allow
         
         'Activate/deactivate cut, copy, paste and pastespecial shortcut keys
        With Application
            Select Case Allow
            Case Is = False
                .OnKey "^c", "CutCopyPasteDisabled"
                .OnKey "^v", "CutCopyPasteDisabled"
                .OnKey "^x", "CutCopyPasteDisabled"
                .OnKey "+{DEL}", "CutCopyPasteDisabled"
                .OnKey "^{INSERT}", "CutCopyPasteDisabled"
            Case Is = True
                .OnKey "^c"
                .OnKey "^v"
                .OnKey "^x"
                .OnKey "+{DEL}"
                .OnKey "^{INSERT}"
            End Select
        End With
    End Sub
     
    Sub EnableMenuItem(ctlId As Integer, Enabled As Boolean)
         'Activate/Deactivate specific menu item
        Dim cBar As CommandBar
        Dim cBarCtrl As CommandBarControl
        For Each cBar In Application.CommandBars
            If cBar.Name <> "Clipboard" Then
                Set cBarCtrl = cBar.FindControl(ID:=ctlId, recursive:=True)
                If Not cBarCtrl Is Nothing Then cBarCtrl.Enabled = Enabled
            End If
        Next
    End Sub
     
    Sub CutCopyPasteDisabled()
         'Inform user that the functions have been disabled
        MsgBox "Sorry!  Cutting, copying and pasting have been disabled in this workbook!"
    End Sub

    '*** In the ThisWorkbook Module ***


    Option Explicit
     
    Private Sub Workbook_Activate()
        Call ToggleCutCopyAndPaste(False)
    End Sub
     
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
        Call ToggleCutCopyAndPaste(True)
    End Sub
     
    Private Sub Workbook_Deactivate()
        Call ToggleCutCopyAndPaste(True)
    End Sub
     
    Private Sub Workbook_Open()
        Call ToggleCutCopyAndPaste(False)
    End Sub

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    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'

  4. #4
    VBAX Expert Logit's Avatar
    Joined
    Sep 2016
    Posts
    613
    Location
    Echo ?


  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    Quote Originally Posted by stuartgb100 View Post
    Hi,

    In a protected workbook, how do I stop the copying of a sheet(s) to a new workbook, please ?

    Thanks.
    Q: do you mean copying a Worksheet to another workbook (Worksheets("Sheet1").Copy) or copying/pasting cells between workbooks?
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    Quote Originally Posted by Paul_Hossler View Post
    Q: do you mean copying a Worksheet to another workbook (Worksheets("Sheet1").Copy) or copying/pasting cells between workbooks?
    Thanks all for the response.

    I'm trying to stop a user from creating their own copy of my workbook.

    I have a project distributed as a template.
    They open the template and are 'forced' to enable macros.
    They are then 'forced' to save as a new macro-enabled workbook.
    Assuming they enable macros, all is fine.

    Using the template method allows me to make improvements to the code as situations arise, or to add new features as and when. I then issue the new template.

    however, some users have simply been making a copy of their workbook, editing it to remove previous data, and then carrying on as if it were a new book.

    This means they are avoiding the new template.

    In the new template code, I store in a hidden sheet the name the user chooses for the workbook they create. I check that name in the open event. If it's different the workbook closes.
    So if user makes a copy of the file via file explorer etc (and changes the filename) then it will close.

    I'm now trying to stop someone from constructing their own book from scratch.

    Seems to me they could open a new xlsm workbook and then copy/move sheets into the new book ?

    Thanks.

  7. #7
    Just to be clear then, the immediate issue is this:

    When a workbook is open, how do I stop the move/copy option of a worksheet to another workbook,
    but allow move/copy sheets within the parent workbook ?

    Thanks.

  8. #8
    I've faced a similar situation with regards to templates being deployed to a large user group and I've yet to come across a perfect answer so I'll be interested to see if anyone here has a clever idea. The choice usually comes down to using various nagging mechanisms and voluntary compliance, or else trying to enforce things via excel's protection mechanisms. Protecting the workbook structure will block copying to another workbook. What is the downside of accepting the tradeoff of also blocking move/copy within the parent workbook? If your template is well designed, you should be able to minimize the need for the user to create new worksheets. And you can still provide a programmatic method of adding a new sheet if that seems warranted.

  9. #9
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Woud putting this in the ThisWorkbook module of the workbook do what you want?
    One can copy paste within the book, or copy from another source to the book, but not from the book to another workbook.
    Private Sub Workbook_Deactivate()
        Application.CutCopyMode = False
    End Sub
    (This does not prevent SaveAs)

    BTW, Excel is not a secure platform. AFAIK, there is no way to securely protect your worksheets from being used as the user wants them to. (There are trivially simple ways to copy your data that can't be discussed on the forum.) Your effort might be better spent on how to get the updates to the copied workbooks rather than preventing their copying. If the user wants a copy, make it so the user gets what they want. Remember the coder is the servant of the user, not the reverse.

  10. #10
    VBAX Expert Logit's Avatar
    Joined
    Sep 2016
    Posts
    613
    Location
    Remember the coder is the servant of the user, not the reverse.
    WHAT !!!!????

    OMG !

    (Thank you for that. Brightened my day ... I think ?)

Posting Permissions

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