+ Reply to Thread
Results 1 to 7 of 7

Thread: I need a macro to lock only cells that a contain a formula

  1. #1
    Registered User
    Join Date
    01-20-2011
    Location
    Hants, UK
    MS-Off Ver
    Excel 2010
    Posts
    6

    I need a macro to lock only cells that a contain a formula

    Many of my spreadsheets are very basic and are used by people who have no knowledge of spreadsheets (if I said "dont overtype a formula", they wouldnt know what I was talking about). I have tried putting comments that say "please dont type in this column/row/cell" to no avail.
    I still get the spreadsheet back saying "its stopped working".

    99% of the time it is because they have overtyped a formula.

    Before I ship a worksheet to them I now lock and protect any ( and only) those cells that contain a formula by:

    1) click on cell 0,0 to highlight all cells in the spreadsheet
    2) click on format cells -> unlock cells
    3) show all cells containing formulas by using keys cntrl + `
    4) highlight all these cells
    5) click on format cells -> lock cells
    6) click on format cells -> protect sheet ( no password)

    I would like to use a general macro to do the above for any worksheet. Any ideas on how to select and highlight only cells which contain a formula?
    Last edited by roybranagan; 01-19-2012 at 04:35 AM.

  2. #2
    Forum Guru
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Excel 2010
    Posts
    1,502

    Re: I need a macro to lock only cells that a contain a formula

    Roy,

    I think if I get you started you can do this one yourself. This bit of code:
    ActiveSheet.Cells.SpecialCells(xlCellTypeFormulas).Select
    will select any cell on the activesheet containing a formula.

    So, what I would do is turn on your macro recorder and run through your whole routine (as outlined earlier), knowing that parts 3 and 4 will be covered by the code I've provided. The recorded macro, with the code provided, should be the basis for your "general" macro. Let me know if more help is needed.
    Last edited by jomili; 01-17-2012 at 09:40 AM.

  3. #3
    Registered User
    Join Date
    01-20-2011
    Location
    Hants, UK
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: I need a macro to lock only cells that a contain a formula

    Jomili,
    excellent suggestion - it seems to work for all but one test. When I have a sheet without any formula in a cell the macro fails with Run-time error '1004' no cells were found
    Roy

  4. #4
    Valued Forum Contributor
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    Excel 2010
    Posts
    522

    Re: I need a macro to lock only cells that a contain a formula

    On Error Resume Next
    ActiveSheet.Cells.SpecialCells(xlCellTypeFormulas).Select
    On Error GoTo 0

  5. #5
    Forum Guru
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Excel 2010
    Posts
    1,502

    Re: I need a macro to lock only cells that a contain a formula

    Nilem showed the way. Try this as the full macro (untested):
    Sub ProtectFormulas()
    '---------------------------------------------------------------------------------------
    ' Procedure : ProtectFormulas
    ' Author    : Jomili
    ' Date      : 1/18/2012
    ' Purpose   : Unlocks non-formula cells, locks formula cells, protects sheet.
    '---------------------------------------------------------------------------------------
        Cells.Locked = False
        On Error Resume Next
        ActiveSheet.Cells.SpecialCells(xlCellTypeFormulas).Locked = True
        On Error GoTo 0
        ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
    End Sub

  6. #6
    Registered User
    Join Date
    01-20-2011
    Location
    Hants, UK
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: I need a macro to lock only cells that a contain a formula

    Jomili and Nilem
    Many thanks - that worked fine - I will continue with a few more tests and let you know tomorrow

  7. #7
    Registered User
    Join Date
    01-20-2011
    Location
    Hants, UK
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: I need a macro to lock only cells that a contain a formula

    Thanks guys
    just what I needed

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0