Password prompt only when edit the cell

vaibhavc

New Member
Joined
Jun 21, 2014
Messages
27
Hi

I have a macro that locks the cell (cell rangeis given) after entering data into cell & thereafter if we have tochange the value of cell it asks for password. It works well.

I want somechanges in this macro - currently if I move cursor on locked cell then alsoit prompt for password & i want if i edit that cell by double click or F2then only he ask for password.

Any help achieving this would be greatly appreciated...
icon_smile.gif


Thanks in Advance !!!

Code:
[COLOR=#333333]Private Sub Worksheet_Change(ByVal Target As Range)[/COLOR]

Dim changed As Range 
Set changed = Intersect(Target, Range("AK4:AK240"))   
If Not changed Is Nothing Then    
If TargetLocked <> True Then              
ActiveSheet.Unprotect ("123456789")       
Target.Locked = True       
ActiveSheet.Protect ("123456789")           
Else                            
End If
End If
End Sub  

Private Sub Worksheet_SelectionChange(ByVal Target As Range)    
Dim Pword As String    
Dim changed As Range         
Set changed = Intersect(Target, Range("AK4:AK240"))        
If Not changed Is Nothing Then            
If Target.Locked = True Then                                       
Pword = InputBox("Enter Password", "Welcome"")                
On Error GoTo Getout                
ActiveSheet.Unprotect Pword            
Target.Locked = False                                
ActiveSheet.Protect Pword                               
End If            
End If    
Exit Sub
Getout: MsgBox "Wrong Password", vbCritical, "Your Busted"     
[COLOR=#333333]End Sub
[/COLOR]

 
Last edited:

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Try:

Code:
[COLOR=#333333]Private Sub Worksheet_Change(ByVal Target As Range)[/COLOR]

Dim changed As Range 
Set changed = Intersect(Target, Range("AK4:AK240"))   
If Not changed Is Nothing Then    
If TargetLocked <> True Then              
ActiveSheet.Unprotect ("123456789")       
Target.Locked = True       
ActiveSheet.Protect ("123456789")           
Else                            
Pword = InputBox("Enter Password", "Welcome"")                
On Error GoTo Getout                
ActiveSheet.Unprotect Pword            
Target.Locked = False                                
ActiveSheet.Protect Pword        
End If
End If
Exit Sub
Getout: MsgBox "Wrong Password", vbCritical, "Your Busted"
    
End Sub


And get rid of SelectionChange sub
 
Upvote 0

Forum statistics

Threads
1,214,431
Messages
6,119,458
Members
448,899
Latest member
maplemeadows

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