VBA Lock Cells under certain conditions

drefiek2

Board Regular
Joined
Apr 23, 2023
Messages
59
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hi folks,
I have a spreadsheet which is user-editable (unlocked cells only - other cells are locked with password protection).
I have two option buttons; when one of them is toggled, several cells are programmed with vba to .clearcontents. When the other option button is toggled those same cells are programmed with vba to .value = 1, and are therefore all populated again with the number 1. The user can toggle between each option button as and when they please.
I need some code for the following: when the .clearcontents option button is toggled, I want those cleared cells to then immediately become LOCKED like all the other locked cells, until such a time where the .value = 1 option button is toggled again when they will unlock, and vice versa.
Essentially I do not want users to be able to click on or enter anything into those .clearedcontents cells whilst that option button is toggled.
Thanks.
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
when the .clearcontents option button is toggled, I want those cleared cells to then immediately become LOCKED
For that, use this code, change "abc" to the password of your sheet.
You will also need to adjust the cells you need to lock. I am only giving an example, because I don't know which cells you want.
VBA Code:
Sub locked_cells()
  ActiveSheet.Unprotect "abc"
    Range("B5,C6,C7,C8,C9,D10,F10:F13").Locked = True
  ActiveSheet.Protect "abc"
End Sub

until such a time where the .value = 1 option button is toggled again when they will unlock
For that, use this code, change "abc" to the password of your sheet.
You must also adjust the cells.
VBA Code:
Sub unlocked_cells()
  ActiveSheet.Unprotect "abc"
    Range("B5,C6,C7,C8,C9,D10,F10:F13").Locked = False
  ActiveSheet.Protect "abc"
End Sub

If you need help fitting the above macros to your codes, then you should put your full code here. ;)

--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------​
 
Upvote 0
Solution
For that, use this code, change "abc" to the password of your sheet.
You will also need to adjust the cells you need to lock. I am only giving an example, because I don't know which cells you want.
VBA Code:
Sub locked_cells()
  ActiveSheet.Unprotect "abc"
    Range("B5,C6,C7,C8,C9,D10,F10:F13").Locked = True
  ActiveSheet.Protect "abc"
End Sub


For that, use this code, change "abc" to the password of your sheet.
You must also adjust the cells.
VBA Code:
Sub unlocked_cells()
  ActiveSheet.Unprotect "abc"
    Range("B5,C6,C7,C8,C9,D10,F10:F13").Locked = False
  ActiveSheet.Protect "abc"
End Sub

If you need help fitting the above macros to your codes, then you should put your full code here. ;)

--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------​
Hi Dante,
Thanks so much again for your help, it works perfectly!
Much appreciated, as usual :)
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,320
Members
452,635
Latest member
laura12345

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