Trying to clear all unprotected cells in a workbook

hnswart

New Member
Joined
Aug 23, 2014
Messages
9
Hi all.

I am new to macros. I uses a macro to clear all unprotected cells in a workbook. It works perfectly in older versions. When i run the macro in 2016 it does nothing.

Here is the code:

Sub ClearUnlockedCells()

Dim wks As Worksheet

For Each wks In ThisWorkbook.Worksheets
On Error Resume Next
wks.UsedRange.Value = vbNullString
Err.Clear: On Error GoTo -1: On Error GoTo 0
Next wks

Set wks = Nothing

End Sub

Please help
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
That code works for me with 365 & should work in 2016.
Is the code located in the workbook that you are trying to clear?
 
Upvote 0
You could try:
VBA Code:
Public Sub ClearUnlockedCells()
    Dim wks     As Excel.Worksheet
    Dim rng     As Excel.Range
    
    For Each wks In ThisWorkbook.Worksheets
        For Each rng In wks.UsedRange.Cells
            If Not rng.Locked Then rng.ClearContents
        Next rng
    Next wks
End Sub
 
Upvote 0
Public Sub ClearUnlockedCells() Dim wks As Excel.Worksheet Dim rng As Excel.Range For Each wks In ThisWorkbook.Worksheets For Each rng In wks.UsedRange.Cells If Not rng.Locked Then rng.ClearContents Next rng Next wks End Sub

Thank you for this code. I get the following error when i run the macro. Please see image
 

Attachments

  • Error.PNG
    Error.PNG
    7.6 KB · Views: 6
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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