Click here to Skip to main content
16,018,802 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Not looking for Password Protection or event .Is there any method like public sub Protect()
under this we can do it .Not in event .

What I have tried:

Public Sub Worksheet_SelectionChange()
Dim xRg As Range
Dim xr As Range
Dim xRgEx As Range
Dim xRgExEach As Range
On Error Resume Next
Application.ScreenUpdating = False
Set xr = Sheets("Sheet1").Range("A:N,1:15")
Set xRg = Sheets("Sheet1").Range("M:Z,15:30") 'Change the row range and column range you will lock without protecting worksheet
Set xRgEx = Application.Intersect(xr, xRg)
If xRgEx Is Nothing Then Exit Sub
Cells(1, 1).Select 'Specify a cell you will shift to after selecting the locked cells
Application.ScreenUpdating = False
End Sub

but My excel range is L18 to AI 50
Posted
Updated 27-May-22 5:45am
Comments
Richard MacCutchan 27-May-22 11:23am    
Select the correct range first, then use rng.Locked = True.
Thirumadhi T Johnson 27-May-22 14:57pm    
Public Sub hi()
Dim xRg As Range
Dim xr As Range
Dim xRgEx As Range
Dim xRgExEach As Range
On Error Resume Next
Application.ScreenUpdating = False
Set xr = Sheets("Sheet1").Range("A:N,1:15")
xr.Locked = True
'Set xRg = Sheets("Sheet1").Range("M:Z,15:30") 'Change the row range and column range you will lock without protecting worksheet
'Set xRgEx = Application.Intersect(xr, xRg)
'If xRgEx Is Nothing Then Exit Sub
'Cells(1, 1).Select 'Specify a cell you will shift to after selecting the locked cells
Application.ScreenUpdating = False
End Sub
Thirumadhi T Johnson 27-May-22 14:57pm    
I tried this way but nothing is getting locked @Richard my task is to lock the entire worksheet if it unprotected mode too but not through any event but calling any method such as public sub Protectcell() like this .
Richard MacCutchan 28-May-22 3:55am    
See the link I posted in my solution below. It explains how to protect a complete worksheet.
Thirumadhi T Johnson 30-May-22 3:48am    
but Richard if I click on Unprotect option in excel it allows the users to edit again no matter if cell is locked too the same cell we can do anything it is unprotected sheet.

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900