Consulting

Results 1 to 6 of 6

Thread: Solved: Solved....Locking Cells?

  1. #1
    VBAX Newbie
    Joined
    Jun 2008
    Posts
    5
    Location

    Solved: Locking Cells?

    I am a VB noob, so I am wondering what needs to be changed in this code to allow me to lock all the cells in every sheet, except for cells that are yellow.

    [vba]Dim wSht As Worksheet
    Dim allwShts As Sheets
    Set allwShts = Worksheets
    On Error Resume Next
    For I = 1 To Sheets.Count
    Sheets(I).Activate
    ActiveSheet.Unprotect Password:=""
    Application.ScreenUpdating = False
    Cells.Select
    Selection.Locked = False
    Selection.FormulaHidden = False
    ActiveSheet.UsedRange.SpecialCells(xlCellTypeConstants).Select
    Selection.Locked = True
    Selection.FormulaHidden = True
    ActiveSheet.UsedRange.SpecialCells(xlCellTypeFormulas).Select
    Selection.Locked = True
    Selection.FormulaHidden = True
    ActiveSheet.Protect Password:=""
    Application.ScreenUpdating = True
    Next I[/vba]
    Last edited by davisbrc; 01-30-2009 at 11:43 AM.

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Try this:
    [VBA]Option Explicit
    Sub ColLock()
    Dim Cel As Range, c As Range, Col As Long, Lkd As Boolean
    Dim sh As Worksheet
    Set Cel = Range("A1")
    Application.ScreenUpdating = False
    Lkd = Cel.Locked
    For Each sh In Worksheets
    sh.Unprotect
    For Each c In sh.UsedRange
    If c.Interior.ColorIndex = 6 Then
    c.Locked = Not Lkd
    End If
    Next
    Next
    For Each sh In Worksheets
    sh.Protect
    Next
    Application.ScreenUpdating = True
    End Sub
    [/VBA]
    example attached
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Why not create a Style for fill colour and "not locked". Saves the problem arising in the future.
    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 Newbie
    Joined
    Jun 2008
    Posts
    5
    Location
    Thanks Lucas and MD for the quick responses.

    I used the code Lucas provided and it worked in my spreadsheet after I got rid of a couple merged cells, which gave me a runtime error code of '1004'

    Thanks for your help.

  5. #5
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    I suggest you avoid merged cells at all costs if possible, this is a perfect example of why. Use center across selection instead.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  6. #6
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Be sure to mark your thread solved if you have your solution. Use the thread tools at the top of the page.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

Posting Permissions

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