Clear multiple range contents, multiple protected sheets, vba

Lelewiwi

Board Regular
Joined
Nov 8, 2023
Messages
50
Office Version
  1. 365
Platform
  1. Windows
I have some code that works great. However, I need to add in another range of cells to be cleared on a different sheet. Sheet names are "Sheet1" and "Hidden". The ranges that need to be cleared on the Hidden sheet are A2, A5, A14.

VBA Code:
Sub ClearCells()

ActiveSheet.Unprotect Password:="232323"

Range("R6").ClearContents
Range("H8").ClearContents
Range("AL8").ClearContents
Range("H9").ClearContents
Range("AN9").ClearContents

Range("F23").ClearContents
Range("F26:F28").ClearContents
Range("N23").ClearContents
Range("N26:N28").ClearContents
Range("V23").ClearContents
Range("V26:V28").ClearContents
Range("AD23").ClearContents
Range("AD26:AD28").ClearContents
Range("AL23").ClearContents
Range("AL26:AL28").ClearContents
Range("AT23").ClearContents
Range("AT26:AT28").ClearContents

ActiveSheet.Protect Password:="232323"

End Sub

Thanks for your help!
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Perhaps something like this:
VBA Code:
Sub ClearCells()
    With ActiveWorkbook
        With .Worksheets("Sheet1")
            .Unprotect Password:="232323"

            .Range("R6").ClearContents
            .Range("H8").ClearContents
            .Range("AL8").ClearContents
            .Range("H9").ClearContents
            .Range("AN9").ClearContents

            .Range("F23").ClearContents
            .Range("F26:F28").ClearContents
            .Range("N23").ClearContents
            .Range("N26:N28").ClearContents
            .Range("V23").ClearContents
            .Range("V26:V28").ClearContents
            .Range("AD23").ClearContents
            .Range("AD26:AD28").ClearContents
            .Range("AL23").ClearContents
            .Range("AL26:AL28").ClearContents
            .Range("AT23").ClearContents
            .Range("AT26:AT28").ClearContents

            .Protect Password:="232323"
        End With

        With .Worksheets("Hidden")
            .Unprotect Password:="232323"

            .Range("A2").ClearContents
            .Range("A5").ClearContents
            .Range("A14").ClearContents

            .Protect Password:="232323"
        End With
    End With
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,593
Messages
6,125,715
Members
449,254
Latest member
Eva146

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