Clear all cells that are unlocked except for one

justundertall

New Member
Joined
Mar 22, 2021
Messages
21
Office Version
  1. 2013
Platform
  1. Windows
Good Day,

I have a workbook for doing up estimates based on the info that is put into the Estimate Spreadsheet.
It is currently has a Button with a Macro for Clearing all unlocked cells(yellow cells) which works great, except I have one cell E5(RedCell) which has a macro to create a new estimate number every time the workbook is opened. In order for the Macro in E5 to work it has to be unlocked and always has to have a number in the cell. So when i click on the clear cells button it clears out the E5 cell.
Is there a way to make it clear all the Cells that are unlocked except for E5?

Here is a picture of my spreadsheet then of the clear cells macro
1616899871168.png


Sub ClearUnlockedCells()
Dim WorkRange As Range
Dim Cell As Range
Set WorkRange = ActiveSheet.UsedRange
For Each Cell In WorkRange
If Cell.Locked = False Then Cell.Value = ""
Next Cell
End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
You could always take the clunky but quick way of assigning a variable to cell E5's contents, clear the cells as you are doing, and reinstate the value in cell E5.

Sub ClearUnlockedCells()
Dim WorkRange As Range, strQuoteNumber as String
strQuoteNumber = Range("E5").Value
Dim Cell As Range
Set WorkRange = ActiveSheet.UsedRange
For Each Cell In WorkRange
If Cell.Locked = False Then Cell.Value = ""
Next Cell
Range("E5").Value = strQuoteNumber
End Sub

If anyone is wondering why I opted for the String type variable, it will work as well for numbers as for text, and in my experience I would not count on digits, and only digits, to be dutifully present in the cell. Sometimes it may, by mistake or design, include a space or an alpha character.
 
Upvote 0
Or
VBA Code:
Sub ClearUnlockedCells()
Dim WorkRange As Range, Cell As Range
Set WorkRange = ActiveSheet.UsedRange
Cells(5, 5).Locked = True
    For Each Cell In WorkRange
        If Cell.Locked = False Then Cell.Value = ""
    Next Cell
Cells(5, 5).Locked = False
End Sub
 
Upvote 0
Or without using a loop...
VBA Code:
Sub Test()
  Dim KeepMe As String
  KeepMe = Range("E5").Value
  Application.FindFormat.Clear
  Application.FindFormat.Locked = False
  ActiveSheet.UsedRange.Replace "", "", xlWhole, , , , True, False
  Application.FindFormat.Clear
  Range("E5").Value = KeepMe
End Sub
 
Upvote 0
Solution
Or without using a loop...
VBA Code:
Sub Test()
  Dim KeepMe As String
  KeepMe = Range("E5").Value
  Application.FindFormat.Clear
  Application.FindFormat.Locked = False
  ActiveSheet.UsedRange.Replace "", "", xlWhole, , , , True, False
  Application.FindFormat.Clear
  Range("E5").Value = KeepMe
End Sub
This worked

Thanks!!
 
Upvote 0

Forum statistics

Threads
1,215,029
Messages
6,122,760
Members
449,095
Latest member
m_smith_solihull

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