Forum Discussion
How can I erase all non-locked cells in a protected worksheet with one keystroke
HansVogelaar,
Thank you for your quick response. The formula you wrote references Set WorkRange = ActiveSheet.UsedRange; I want to have the formula run on several sheets in the workbook. With my limted knowledge of VBA, I have only ever used something like Sheets("combined all").Select first. Would this be the correct entry to use for each sheet I want and then plug your formula in after each sheet is selected? There are 6 sheets in the workbook and I need to clear 5, 2 have locked cells, 3 do not.
What would you like to clear in a sheet that does not have locked cells?
- YvonneC1190Aug 30, 2024Copper ContributorUnfortunately there could be data in any of those 50,000 cells (10,000 per sheet). At this point, I am going just going to create a master template workbook that is clean/clear of data and create instructions to open the template and immediately do a file save as to ensure they start with a clean sheet. I think that will be much easier. I really appreciate your help with this, and will try it on my smaller work books that have 1/10th the data to clear.
- HansVogelaarAug 30, 2024MVP
YvonneC1190 That means that the code has to loop through some 50,000 cells. That will make it run for an excessively long time.
Is there a way we can limit the ranges the code has to inspect?
- YvonneC1190Aug 30, 2024Copper Contributor
HansVogelaar Thank you. I did try this and updated the array to show all 5 sheets. The macro ran for 3 minutes, and ended after one sheet. I updated it to rearrange the sheet order from left to right rather than right to left, but got the same results. The only sheet that is getting cleared is "workding data". Each sheets if approximately 10,000 cells so is it possible it is just too large?
Here is my change to the array...
' Change the array as needed
For Each ASheet In Worksheets(Array("Critical Illness", "Accident", "Sheet1", "combined all", "working data"))
Set WorkRange = ASheet.UsedRange - HansVogelaarAug 28, 2024MVP
You can use the ClearSpecific macro from one of my previous replies:
Sub ClearSpecific() Dim ASheet As Worksheet Dim WorkRange As Range Dim UnlockedRange As Range Dim Cell As Range If MsgBox("Are you sure you want to clear unlocked cells on specific sheets?", _ vbYesNo + vbQuestion) = vbYes Then ' Change the array as needed For Each ASheet In Worksheets(Array("ThisOne", "ThatOne", "AnotherOne")) Set WorkRange = ASheet.UsedRange For Each Cell In WorkRange If Not Cell.Locked Then If UnlockedRange Is Nothing Then Set UnlockedRange = Cell Else Set UnlockedRange = Union(UnlockedRange, Cell) End If End If Next Cell If Not UnlockedRange Is Nothing Then UnlockedRange.ClearContents End If Next ASheet End If End Sub
Edit Array("ThisOne", "ThatOne", "AnotherOne") so that it lists the five sheets that you want to clear. Each sheet name enclosed in double quotes, and separated by commas.
- YvonneC1190Aug 28, 2024Copper ContributorI need to clear the entire sheet.