+ Reply to Thread
Results 1 to 7 of 7

Problem with Locking cells

Hybrid View

  1. #1
    Registered User
    Join Date
    11-13-2006
    Posts
    16

    Problem with Locking cells

    How can I lock cells that contains formulas AND Text but NOT cells that contain constants (ie. input cells that feed to formulas)? I simply can not get the code below to work.

    I would appreciate your help.
     Dim SI As Worksheet
    On Error Resume Next
    For Each SI In Worksheets
      With SI
        .Unprotect "Password"
        .Cells.Locked = False
        .Cells.SpecialCells(xlCellTypeFormulas).Locked = True
    
        Cells.Select
        Selection.SpecialCells(xlCellTypeConstants, 1).Select
        Selection.Locked = False
    
    
        .Protect "Password"
      End With
    Next SI
    Last edited by VBA Noob; 07-18-2007 at 05:05 PM.

  2. #2
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    How about

    Cells.SpecialCells(xlCellTypeFormulas).Locked = True
    Cells.SpecialCells(xlCellTypeConstants, 2).Locked = True
    That will lock the text and formula cells.

    HTH

    rylo

  3. #3
    Registered User
    Join Date
    11-13-2006
    Posts
    16
    Thanks for the input Rylo. I have tried that before.
    But I can not get that into the code below and work.

    Can you see anything wrong with the syntax?

  4. #4
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    I think you forgot to add your code.


    rylo

  5. #5
    Registered User
    Join Date
    11-13-2006
    Posts
    16
    Thanks Rylo,

    Here is the code.

    Dim SI As Worksheet
    On Error Resume Next
    For Each SI In Worksheets
    With SI
    .Unprotect "Password"
    .Cells.Locked = False
    .Cells.SpecialCells(xlCellTypeFormulas).Locked = True

    Cells.Select
    Selection.SpecialCells(xlCellTypeConstants, 1).Select
    Selection.Locked = False


    .Protect "Password"
    End With
    Next SI

  6. #6
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    The code below will lock all cells on the sheet, then unlock only the constats.
    Sub aaa()
      Dim SI As Worksheet
      On Error Resume Next
      For Each SI In Worksheets
        With SI
          .Unprotect "Password"
          .Cells.Locked = True
          .Cells.SpecialCells(xlCellTypeConstants, 1).Locked = False
          .Protect "Password"
        End With
      Next SI
    
    End Sub
    This code will unlock all cells, and then lock the formulas and text.
    Sub bbb()
      Dim SI As Worksheet
      On Error Resume Next
      For Each SI In Worksheets
        With SI
          .Unprotect "Password"
          .Cells.Locked = False
          .Cells.SpecialCells(xlCellTypeFormulas).Locked = True
          .Cells.SpecialCells(xlCellTypeConstants, 2).Locked = True
          .Protect "Password"
        End With
      Next SI
    
    End Sub
    rylo

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.6.0 RC 1