Clear All Unprotected Cells (merged too)

jamesmev

Board Regular
Joined
Apr 9, 2015
Messages
233
Office Version
  1. 365
Platform
  1. Windows
Would anyone know how to include merged cells into this clear?
I need to clear all unprotected cells in a worksheet. (if you have something that already works would you share?)

Code:
Sub cleartestall()    Dim r As Range, rcleara As Range
    Set rclear = Nothing
    For Each r In ActiveSheet.UsedRange
        If r.Locked = False Then
            If rcleara Is Nothing Then
                Set rcleara = r
            Else
                Set rcleara = Union(rcleara, r)
            End If
        End If
    Next r
    rcleara.clear
End Sub
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Here is a method to loop merged cells and clear them individually
Code:
Sub ClearMergedCells()
    Dim Rng As Range, cel As Range
    Set Rng = ActiveSheet.UsedRange
    For Each cel In Rng
        If cel.MergeCells Then
          If cel.Locked = False Then cel.MergeArea.ClearContents
        End If
    Next cel
End Sub

Or (using UNION) clearing them together

Code:
Sub ClearMergedCells()
    Dim Rng As Range, cel As Range, rClearA As Range
    Set Rng = ActiveSheet.UsedRange
    For Each cel In Rng
        If cel.MergeCells And cel.Locked = False Then
            If rClearA Is Nothing Then
                Set rClearA = cel
            Else
                Set rClearA = Union(rClearA, cel)
            End If
        End If
    Next cel
    rClearA.ClearContents
End Sub


Add a second loop to your procedure to deal with merged cells separately

Note
ClearContents used to avoid removing the merged cell completely
Use Clear if that is what you want to happen
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,878
Messages
6,181,528
Members
453,053
Latest member
DavidKele

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