Worksheet SelectionChange locking sheet

drag1c

Board Regular
Joined
Aug 7, 2019
Messages
92
Office Version
  1. 2016
  2. 2013
Platform
  1. Windows
Hi,

I am using code for sheet lock, but if selection goes (in this specific case) from A6 to A1 ( -> Range("A6:A1") <- ) it will keep unlocked file which is wrong.

If there is any kind of intersection with A6:AN35, it should not allow file to be unlocked. How to change this code into that?
Code:
Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Const WS_RANGE As String = "A6:AN35"     '<== change to suit

    On Error GoTo ws_exit
    Application.EnableEvents = False

    If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then
        With Target
        
            Call UnlockMe
        End With
    Else
        With Target
        
            Call LockMe
        End With
    End If

ws_exit:
    Application.EnableEvents = True
End Sub



Private Sub UnlockMe()
Sheet2.Unprotect "123"
End Sub

Private Sub LockMe()
Sheet2.Protect "123"
End Sub
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Switch these 2 lines
Call UnlockMe
Call LockMe
or if you don't want to lock the sheet when there is an intersect, use Exit Sub.
 
Upvote 0
Switch these 2 lines
Call UnlockMe
Call LockMe
or if you don't want to lock the sheet when there is an intersect, use Exit Sub.
It doesn't work properly. I've set Calls right but there should be an addition:
If selected range is in both ranges, it should lock cells.

If any cell in range A6:AN35 is selected or any range in A6:AN35 is selected -> Unlock
If any cell in range A6:AN35 is selected but range is extended to cells outside of A6:AN35 -> Lock
If any cell outside of range A6:AN36 is selected -> Lock
If any cell outside of range A6:AN36 is selected but range is extended to cells inside A6:AN35 -> Lock

Example of lock:
1676971012261.png


Example of unlock:
1676971045517.png



Do you have an idea how to do it?
 
Upvote 0
I've wrote something like this, and it seems works:
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Const LandscapeRange As String = "A6:AN35"

    On Error GoTo ws_exit
    Application.EnableEvents = False

    If Not Intersect(Target, Me.Range(LandscapeRange)) Is Nothing Then
         With Target
            Call UnlockMe
        End With
    Else
        With Target
            Call LockMe
        End With
    End If

    If Not Intersect(Target, Me.Range("A1:AN5")) Is Nothing Then
        With Target
            Call LockMe
        End With
    End If

    If Not Intersect(Target, Me.Range("A36:AN45")) Is Nothing Then
        With Target
            Call LockMe
        End With
    End If


ws_exit:
    Application.EnableEvents = True
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,584
Messages
6,120,387
Members
448,957
Latest member
Hat4Life

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top