Consulting

Results 1 to 13 of 13

Thread: Lock Cell/Cell Range based on Cell value

  1. #1
    VBAX Regular
    Joined
    Apr 2019
    Posts
    17
    Location

    Lock Cell/Cell Range based on Cell value

    Hi,

    I want to lock cell ref. B2 and cell range ref. C1 to C10 based on cell value selected in A1. Now, A1 column has validation, when Apple is selected B2 has to be locked from editing and when Mango is selected cell range C1 to C10 has to be locked from editing.

    Please see the attached workbook for your ready reference.

    Regards,
    Uday
    Attached Files Attached Files

  2. #2
    VBAX Regular
    Joined
    Apr 2019
    Posts
    17
    Location
    Please help!!

  3. #3
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,055
    Location
    How did you propose to then unlock the cell B2 and Range C1:C10?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  4. #4
    VBAX Mentor
    Joined
    Dec 2008
    Posts
    404
    Location
    It is true that not enough data about other cells of the sheet, but let's start with such a procedure (place in the sheet module, not in the standard module!)
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Not Intersect(Target, Me.Range("A1")) Is Nothing Then
            Me.Unprotect
            Me.Cells.Locked = False
    
    
            If Me.Range("A1").Value = "Apple" Then
                Me.Range("B2").Locked = True
                Me.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
                           False
            ElseIf Me.Range("A1").Value = "Mango" Then
                Me.Range("C1:C10").Locked = True
                Me.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
                           False
            End If
        End If
    End Sub
    Artik

  5. #5
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,055
    Location
    Try adding this in a sheet module along with the code suggested by Artik. The purpose here is to give you an unlock option by double clicking cell A2.

    Private Sub Worksheet_BeforeDoubleClick(ByVal Target as Range, Cancel as Boolean)
       If not Intersect(Target , Me.Range("A2")) then Exit Sub  <---- change this to suit your needs     
          If(And(Me.Range("B2" & "C1:C10").Locked = True Then
          Me.Range("B2" & "C1:C10").Locked = False
          End If
       End If
    End Sub
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  6. #6
    VBAX Mentor
    Joined
    Dec 2008
    Posts
    404
    Location
    Aussiebear, probably something wrong with your code.

    Artik

  7. #7
    VBAX Regular
    Joined
    Apr 2019
    Posts
    17
    Location
    Hi Artik,

    B2 is not locking as per the condition set. please help.

    Regards,
    Uday

  8. #8
    VBAX Regular
    Joined
    Apr 2019
    Posts
    17
    Location
    Hi Aussiebear,

    Thanks for your response, however the code is not working and giving compiler error.

    Regards,
    Uday

  9. #9
    VBAX Mentor
    Joined
    Dec 2008
    Posts
    404
    Location
    Quote Originally Posted by shamim View Post
    B2 is not locking as per the condition set.
    If you select "Apple" in cell A1, access to cell B2 is blocked. If you select "Mango", then B2 is unlocked and the range C1: C10 is blocked. If you clear cell A1 - all cells are unlocked.
    Is this not consistent with the assumptions?

    Artik

  10. #10
    VBAX Regular
    Joined
    Apr 2019
    Posts
    17
    Location
    Hi Artik,

    Thanks for your quick response. If I select the Apple in A1, access to cell B2 is not blocking.

    Regards,
    Uday,

  11. #11
    VBAX Mentor
    Joined
    Dec 2008
    Posts
    404
    Location
    Please attach the test file in which there is a B2 blocking problem. For me, the code shown works.

    Artik

  12. #12
    VBAX Regular
    Joined
    Apr 2019
    Posts
    17
    Location
    Hi Artik,

    I have attached the file. please check it.

    Regards,
    Uday
    Attached Files Attached Files

  13. #13
    VBAX Mentor
    Joined
    Dec 2008
    Posts
    404
    Location
    For me, it works according to my earlier assumptions.

    Artik

Posting Permissions

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