UsedRange limits the search to a certain range (used range), so the whole sheet is not searched. However, if the used range is significantly larger than the range N3: AZ500, then in fact quite a lot of unnecessary cells are processed. To limit the scope of searches, we can use the SpecialCells property. I don't know what type of data to expect in unlocked cells. If they are only constant values, you can search the range:
ActiveSheet.UsedRange.SpecialCells(xlCellTypeConstants)
when there are only formulas, replace the constant with xlCellTypeFormulas. If both types are present, this becomes a problem, create two loops for each type separately. Also note that when using SpecialCells, the sheet cannot be protected at this point.
The ClearUnlockedCells macro has a performance problem, because it cleans the cells one at a time. To speed up its operation, it should be built similarly to ClearMergedCells - create a union.
Assuming that the unprotected cells can contain only constants (without formulas), the sheet is not protected at this point (the photo and the assumption of protection can be added to the procedure) and that we always execute the main macro CLEAN_SHEET, i.e. we want to clean the merged and non-merged cells, the procedure could look like this:
Sub CLEAN_SHEET_1()
Dim Rng As Range, cel As Range, rClearA As Range
Dim i As Long
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With
'Set Rng = Range("N3:AZ500").SpecialCells(xlCellTypeConstants)
'Or
Set Rng = ActiveSheet.UsedRange.SpecialCells(xlCellTypeConstants)
For Each cel In Rng
If Not cel.Locked Then
i = i + 1
If rClearA Is Nothing Then
Set rClearA = cel
Else
Set rClearA = Union(rClearA, cel)
End If
'When a large union of ranges is formed, it is better to clean the range in batches.
If i >= 500 Then
rClearA.Value = Empty
i = 0
Set rClearA = Nothing
End If
End If
Next cel
If Not rClearA Is Nothing Then
rClearA.Value = Empty
End If
With Application
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
End With
MsgBox "Done.", vbInformation
End Sub
Artik
Bookmarks