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

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
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,223,214
Messages
6,170,774
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