Consulting

Results 1 to 6 of 6

Thread: Solved: Lock cells based on cell value

  1. #1
    VBAX Regular
    Joined
    Aug 2009
    Posts
    44
    Location

    Solved: Lock cells based on cell value

    Morning all,

    Is it possible to lock a row based (to prevent further editing) on the value of a single cell?

    I would like to lock columns O:AC if the value of column AB equals Leaver. Likewise I would like to lock columns O:W if the value of column AB equals Increase Post Jan.

    I've tried using the following code (for part one of my question), however, instead of locking the cells mentioned it hides all formulas, locks the worksheet but still permits values to be entered in all cells?!

    [VBA]
    Private Sub Worksheet_Change(ByVal Target As Excel. Range)
    Me. Unprotect
    If UCase(Cells(Target.Row, 29)) = "Leaver" Then
    Cells(Target.Row, 3).Resize(, 28).Locked = True
    Else
    Cells(Target.Row, 3).Resize(, 28).Locked = False
    End If
    Me.Protect
    End Sub
    [/VBA]

    Any ideas? Any help would be much appreciated.

    Thanks.

  2. #2
    VBAX Contributor
    Joined
    Feb 2009
    Posts
    103
    Location
    Hi Sdave, in your statement you put ucase..
    see:
    If UCase(Cells(Target.Row, 29)) = "Leaver" Then

    so that statement will execute the else statement..you compare "LEAVER" to "Leaver" how about changing the "Leaver" to "LEAVER" check what will happen.. so change your statement like this:

    If Ucase(Cells(Target.Row, 29)) = "LEAVER" Then

    try that and check the result Good luck!

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    Me.Unprotect
    Cells(Target.Row, 3).Resize(, 28).Locked = LCase(Cells(Target.Row, 29)) = "leaver"
    Me.Protect
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Regular
    Joined
    Aug 2009
    Posts
    44
    Location
    Thanks joms, why I missed that I don't know?!

    xld - much appreciated, as always. Your modified code works a treat. Is there anyway I could set the case to proper as opposed to either upper or lower, it's just for aesthetic purposes?!

    I've tried a few different connotations trying to insert vbProperCase or Proper but I'm stumped, I keep receiving a compile error!!!

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Private Sub Worksheet_Change(ByVal Target As Excel.Range)

    Me.Unprotect
    Cells(Target.Row, 3).Resize(, 28).Locked = Application.Proper(Cells(Target.Row, 29)) = "Leaver"
    Me.Protect
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    VBAX Regular
    Joined
    Aug 2009
    Posts
    44
    Location
    Thanks xld - much appreciated.

Posting Permissions

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