VBA to protect and unprotect in given range in sheet

0 votes

I used this macro to protect and unprotect a specified range of cells on a sheet, but it's giving me trouble. When I execute this macro, it protects in the specified range of cells A1 to D20, and when I rerun it to unprotect in the specified range, it does not unprotect.

Sub lockcells()
 Dim Rng
 Dim MyCell
 Set Rng = Range("A1:D20")
 For Each MyCell In Rng
 If MyCell.Value = "" Then
 Else: ActiveSheet.UnProtect Password:="123"
 MyCell.Locked = True
 MyCell.FormulaHidden = False
 ActiveSheet.Protect Password:="123", UserInterFaceOnly:=True
 End If
End Sub

I want to protect and unprotect with single macro.

Nov 24, 2022 in Others by narikkadan
• 50,440 points

1 answer to this question.

0 votes

It needs a minor tweak to become "protect/unprotect." I assumed that if a cell is not empty, you only want to lock or safeguard it.

Option Explicit

Sub lockcells()
Dim Rng As Range
Dim MyCell As Object

Set Rng = Range("A1:D20") 'Set range to lock cells

If ActiveSheet.ProtectContents = True Then 'Check if sheet is protected
    ActiveSheet.Unprotect Password:="123" 'Password to unprotect
For Each MyCell In Rng
    If MyCell.Value <> "" Then 'If cell is empty, if not empty lock the cell
        MyCell.Locked = True 'Lock cell
        MyCell.FormulaHidden = False 'Don't hide formulas
    End If
Next MyCell
ActiveSheet.Protect Password:="123", UserInterFaceOnly:=True 'Protect Sheet
End If
End Sub

If you want all cells to be editable except a range you can add the following code:

    ActiveSheet.Cells.Locked = False
    ActiveSheet.Cells.FormulaHidden = False
    'For Each MyCell In Rng
answered Nov 24, 2022 by Kithuzzz
• 27,360 points

Related Questions In Others

0 votes
1 answer

Excel-VBA - How to identify Target range (more than 1 cell) is deleted in a Worksheet_Change function?

You misunderstand the purpose of the function ...READ MORE

answered Sep 23, 2022 in Others by narikkadan
• 50,440 points
0 votes
1 answer

How to print an Excel Sheet using VBA with Nitro PDF Creator in Excel 2016

you can use the built-in excel facilities ...READ MORE

answered Sep 24, 2022 in Others by narikkadan
• 50,440 points
0 votes
1 answer

I want to make Excel read a value in Calc and copy it to my sheet in Excel

Here is the sample code that will allow ...READ MORE

answered Oct 27, 2022 in Others by narikkadan
• 50,440 points
0 votes
0 answers

Data Driven Framework -- how to read and write in excel sheet using Selenium WebDriver with java

I'm using this code to read something, ...READ MORE

Oct 31, 2022 in Others by Kithuzzz
• 27,360 points
0 votes
1 answer

Excel VBA: Open Hyperlinks in a loop and copy paste download link in a sheet

Refer this tutorial for your solution: https://evermap.com/Tutorial_AB ...READ MORE

answered Dec 25, 2022 in Others by narikkadan
• 50,440 points
0 votes
1 answer

Retrieve epay.info Balance with VBA and Excel

This code should log you in, provided ...READ MORE

answered Sep 5, 2018 in Blockchain by digger
• 26,740 points
0 votes
1 answer

How to load file to Excel Power query from SFTP site

Currently, I don't think there is a ...READ MORE

answered Dec 3, 2018 in Power BI by Upasana
• 8,620 points
0 votes
1 answer

Using VBA Excel to create a gramatically correct list

The Excel AND function is a logical ...READ MORE

answered Feb 9, 2022 in Others by gaurav
• 22,940 points
0 votes
2 answers

How to copy a formula horizontally within a table using Excel VBA?

Hi so basically, create an adjacent column ...READ MORE

answered Feb 16, 2022 in Others by Edureka
• 13,640 points
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP