Locking and Unlocking Cells

raship

New Member
Joined
Apr 1, 2014
Messages
3
First of all, I am very very geek for VB codes, so please excuse my poor coding attempts:(

let me explain what I want to achieve.


I have cell A1, which has two text values, "yes" or "no".(by data validation)
Now if value "no" is selected, then I want cells A2:A6 locked and content cleared. and if value is changed to "Yes" then those cells should be unlocked.


following macro I have entered

Sub Lockcell()

If Sheet1!a1 = "No" Then
Range("A2:A6").Select
Selection.Locked = True
Selection.FormulaHidden = False
Selection.ClearContents
End If
End Sub

Obviously, this didnt work. On searching this forum, I got something about activesheet,
and I tried with this


Private Sub Worksheet_Change(ByVal Target As Range)

If ActiveSheet.Cells(1, 1).Text = "No" Then
ActiveSheet.Range(Cells(2, 1), Cells(6, 1)).Locked = True
Else
ActiveSheet.Range(Cells(2, 1), Cells(6, 1)).Locked = False
End If
End Sub


Please give little guidance if active.sheet command to be used.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
the activesheet command don't need to be used on the Workbook Module like Private Sub Worksheet_Change because the code specified in there only runs in this worksheet

if you have a normal module and you want to process some code on the active sheet then you could use the ActiveSheet command if the active sheet is the sheet you want to look through
 
Upvote 0
the activesheet command don't need to be used on the Workbook Module like Private Sub Worksheet_Change because the code specified in there only runs in this worksheet

if you have a normal module and you want to process some code on the active sheet then you could use the ActiveSheet command if the active sheet is the sheet you want to look through

Thanks for reply. I appreciate this. Now problem is I know about excel. But I know very little about VB codes. So whatever you said in your last reply is roman and Greek.
But I will try to understand. Should I put that code in module?
can you give what code will achieve my above mention result?
Thanks
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,688
Members
449,117
Latest member
Aaagu

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