Clearing all unlocked cells on every sheet in a workbook.

jimbofoxman

New Member
Joined
Feb 23, 2018
Messages
13
I have 3 bits of code in the PERSONAL workbook (hidden on start). First two bits unprotect and protect all worksheets. I have a work book with one tab for every day of the Quarter. On the code I got for clearing all unprotected/unlocked cells it's acting like it's not even running. Is it because the sheets are protected? Even though I have certain cells unlocked.

I modified my ribbon to have buttons for each 3 macros from the PERSONAL workbook.

Here is the code I currently have. I have tried several and it seems like it total ignores it, nothing gets cleared.

Code:
    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

Not sure why it acts like it's ignoring it. I've read in several posts about different code for merged cells. I do have some merged cells to deal with too.

Just kinda stumped.

Thanks
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hi,
I suspect your code is throwing an error but masked by the Resume Next error trap you have.

solution untested but try following & see if does what you want

Code:
Sub ClearUnlockedAllSheets()
Dim Cell As Range, ClearUnlockedCells As Range
Dim Sht As Worksheet
  
For Each Sht In Worksheets
    For Each Cell In Sht.UsedRange.Cells
    If Not Cell.Locked Then
        If ClearUnlockedCells Is Nothing Then
            Set ClearUnlockedCells = Cell
        Else
            Set ClearUnlockedCells = Union(ClearUnlockedCells, Cell)
        End If
    End If
    Next Cell
    If Not ClearUnlockedCells Is Nothing Then ClearUnlockedCells.Clear
    Set ClearUnlockedCells = Nothing
Next Sht
End Sub

Dave
 
Upvote 0
Thanks Dave! It worked great. Do you know if their is a way to just clear the data and not the formatting? I have the cells filled with colors so they enter data from the right spots.

Thanks again!

Jim
 
Upvote 0
Change clear to clearcontents.
Btw in your original code it should have been ActiveWorkbook.Worksheets not ThisWorkbook.Worksheets.
 
Upvote 0
Thanks Dave! It worked great. Do you know if their is a way to just clear the data and not the formatting? I have the cells filled with colors so they enter data from the right spots.

Thanks again!

Jim

welcome glad it helped

change part line shown in red

Rich (BB code):
If Not ClearUnlockedCells Is Nothing Then ClearUnlockedCells.ClearContents

Dave
 
Upvote 0

Forum statistics

Threads
1,216,462
Messages
6,130,781
Members
449,591
Latest member
sharmavishnu413

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