Consulting

Results 1 to 10 of 10

Thread: Lock active cell based on value in above cell

  1. #1
    VBAX Newbie
    Joined
    Sep 2007
    Location
    Fort Collins, CO
    Posts
    5
    Location

    Lock active cell based on value in above cell

    Hello. I'm new here, and need an Excel VBA macro that locks the active cell I am in based on whether the value in the cell above is 0. That is, If cellabove = 0, then active cell is locked, but if cellabove => 1 then not locked. Thanks!

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

    With Activecell
    .Locked = (.Offset(-1,0).Value = 0)
    End With
    [/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

  3. #3
    VBAX Newbie
    Joined
    Sep 2007
    Location
    Fort Collins, CO
    Posts
    5
    Location
    I have pasted the following into the module, but it doesn't seem to want to run automatically. Any suggestions? Thanks.

    Sub LockActiveCell()

    With ActiveCell
    .Locked = (.Offset(-1, 0).Value = 0)
    End With

    End Sub

  4. #4
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    You need a worksheet change event for that. Right click your worksheet tab and select View Code, paste this in the code pane...

    [vba]Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub
    Target.Offset(1, 0).Locked = (Target.Value = 0)
    End Sub[/vba]

    HTH

  5. #5
    VBAX Newbie
    Joined
    Sep 2007
    Location
    Fort Collins, CO
    Posts
    5
    Location
    Sorry, but it still doesn't want to work. When I try to input any integer (I'm using 1 to try it out) in the active cell, I get error message: "Unable to set the locked property of the range class", then on to debug which highlights:

    Target.Offset(1, 0).Locked = (Target.Value = 0)

    I am just wanting to stop user input in the active cell if the cell above is 0. Any other number >0 in the cell above allows user input of any integer =>1 in the active cell. Thanks.

  6. #6
    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 Range)
    If Target.Cells.Count > 1 Then Exit Sub
    Me.Unprotect
    Target.Offset(1, 0).Locked = (Target.Value = 0)
    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

  7. #7
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub
    If Target.Value = 0 Then
    ActiveSheet.Unprotect
    Target.Offset(1, 0).Locked = True
    ActiveSheet.Protect
    End If
    End Sub
    [/VBA]
    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'

  8. #8
    VBAX Newbie
    Joined
    Sep 2007
    Location
    Fort Collins, CO
    Posts
    5
    Location
    Still no go. Perhaps I should mention that I'm using Excel 2000 . . .
    I'm thinking that evaluating the cell above the active cell would be offset(-1,0), right?

  9. #9
    VBAX Newbie
    Joined
    Sep 2007
    Location
    Fort Collins, CO
    Posts
    5
    Location
    Still no go. Perhaps I should mention that I'm using Excel 2000 . . .
    I'm thinking that evaluating the cell above the active cell would be offset(-1,0), right?

  10. #10
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Yes, you are correct about the offset, with the row value of -1 being the row directly above the cell in question (i.e. Target). When you use xld's code, where does it error out at and what is the error message?

Posting Permissions

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