Consulting

Results 1 to 8 of 8

Thread: Delete formulas when unprotect

  1. #1

    Delete formulas when unprotect

    Hi All,
    I've been busy with a project to sort of protect my excell program, to the general user anyway. I have coded that when the sheet has been unptotected by a user that should not be unprotecting, then I delete the formulas in the cells, I was wondering if there is a way to immediately close the workbook on unprotect, instead of them having to change a cell first after unprotecting. In other words as soon as the user unprotects the sheet I would like to close the workbook immediately.
    I have coded so far:-
    Sub SheetIsProtected(oSheet As Worksheet)
    With oSheet.UsedRange
       If oSheet.ProtectContents = False _
       Or oSheet.ProtectDrawingObjects = False _
       Or oSheet.ProtectScenarios = False Then
          UserForm3.Show
          .Value = .Value
          ActiveWorkbook.Save
          Application.Quit
       End If
    End With
    End Sub
    Thanks in advance
    Lee
    Last edited by Aussiebear; 04-08-2023 at 04:24 PM. Reason: Adjusted the code tags

  2. #2
    VBAX Contributor CaptRon's Avatar
    Joined
    Mar 2007
    Location
    Austin, Texas
    Posts
    141
    Location
    I don't know of a way to instantly close the workbook without some sort of user action to trigger sheet protection detection. However, it does the user little good to remove protection if they can't alter your work. If you are just trying to keep your formulas hidden, Worksheet_SelectionChange will do the trick. They have to select the cell to peek at the formula.

    When faced with a similar challenge, I try to be only as aggressive as necessary to keep the overly curious out of the works.

    If sheet protection has been removed then I apply one of these approaches:
    1. Close the workbook w/o saving changes
    2. Delete all sheets (except one), save and close
    3. Delete sheets and code

    So far, I've found that alerting the user that sheet protection has been removed then closing down the workbook without saving changes has pretty much taken care of the problem.

    I've attached a sample workbook illustrating the close w/o saving method.

    Ron
    Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, you're a mile away and you have their shoes.

  3. #3
    VBAX Contributor CaptRon's Avatar
    Joined
    Mar 2007
    Location
    Austin, Texas
    Posts
    141
    Location
    Lee,

    Attached is a sample workbook showing the self-destruct (delete all but one sheet) approach.

    It allows the user to print the sheets before deletion. Test on a copy of the workbook or you'll say goodbye to the original.

    Ron
    Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, you're a mile away and you have their shoes.

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    "Nothin's ever sliced so thin, there's not two sides to it."
    except, perhaps, a mobius strip?
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  5. #5
    VBAX Contributor CaptRon's Avatar
    Joined
    Mar 2007
    Location
    Austin, Texas
    Posts
    141
    Location
    Quote Originally Posted by p45cal
    except, perhaps, a mobius strip?
    Maybe so. Can you slice one? If so, how many sides would it have? If it can't be sliced, does it qualify?

    Ron
    Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, you're a mile away and you have their shoes.

  6. #6
    Thanks CaptRon,
    The advise you gave is very useful, and I think that the delete sheets and then save is the way to go.
    Cheers
    Lee

  7. #7
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    A curious user will find out the formulas if they want to. The easiest way to see your formulas will not be affected by that code.

    Sheet Protection, with a password, should be sufficient for the curious, but inexperienced.
    It seems to me, that deleting a sheet (i.e. ruining the spreadsheet) is more likely to harm the authorized user for clumbsyness than to add security.

  8. #8
    VBAX Contributor CaptRon's Avatar
    Joined
    Mar 2007
    Location
    Austin, Texas
    Posts
    141
    Location
    Quote Originally Posted by mikerickson
    Sheet Protection, with a password, should be sufficient for the curious, but inexperienced.
    It seems to me, that deleting a sheet (i.e. ruining the spreadsheet) is more likely to harm the authorized user for clumbsyness than to add security.
    I confess, I have never known of a user inadvertently removing the sheet protection, given a password was applied. Circumventing the password protection must be done purposely and with some determination. For ordinary users content to use the workbook as designed, password sheet protection is more than adequate and this code presents no hazard.

    The approach detailed above is not intended for the ordinary user. Since employing these tactics more than 5 years ago, I no longer have to endure emails and phone calls from users distressed by malfunctioning workbooks "improved" and distributed by some genius too lazy or inept to create his own work but quite sure he knows how to better mine. When just once the workbook disappears before their eyes, word quickly gets around....DON'T REMOVE THE SHEET PROTECTION or the workbook is toast. I do allow them to print each page before destruction (I'm not heartless).


    Another part of my need to preserve the workbook intact is that our employees do not have permission or privilege to alter agency forms/reports or workbooks that produce them, though a few are bold enough to try anyway.

    All my workbooks open with a notice to the user that sheet protection must remain intact or the workbook will either self-destruct or otherwise become dysfunctional. Rather than destroy the worksheets, forcing the workbook to close without saving has proven equally effective, if properly done. Not as dramatic, but still effective.

    Ron
    Last edited by CaptRon; 02-23-2009 at 10:30 PM.
    Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, you're a mile away and you have their shoes.

Posting Permissions

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