Deleting all unprotected cells

mahmed1

Well-known Member
Joined
Mar 28, 2009
Messages
2,302
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi Guys,

I have a workbook that is protected and locked however certain cells are unlocked and are not protected (I.e where i can enter values) etc...

I am trying to add a btton that will clear the form (the values in the unprotected/locked cells).

What is the VBA code to delete all values in the unprotected/locked cells in the workbook?

Many Thanks
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Try this:
Code:
Sub ClearCells()
Dim rngLook As Range, rngC As Range, rngClear As Range
Set rngLook = ActiveSheet.UsedRange
For Each rngC In rngLook.Cells
    If rngC.Locked = False Then
        If rngClear Is Nothing Then
            Set rngClear = rngC
        Else
            Set rngClear = Union(rngClear, rngC)
        End If
    End If
Next rngC

If Not rngClear Is Nothing Then
    rngClear.ClearContents
End If
End Sub
 
Upvote 0
Try this:
Code:
Sub ClearCells()
Dim rngLook As Range, rngC As Range, rngClear As Range
Set rngLook = ActiveSheet.UsedRange
For Each rngC In rngLook.Cells
    If rngC.Locked = False Then
        If rngClear Is Nothing Then
            Set rngClear = rngC
        Else
            Set rngClear = Union(rngClear, rngC)
        End If
    End If
Next rngC
 
If Not rngClear Is Nothing Then
    rngClear.ClearContents
End If
End Sub


Hi All

Sometimes you might accidentally click the button and clear the form. Can i have the code like in an input box giving an option yes or no saying "are you sure you want to clear the data"?

Thanks
 
Upvote 0
Add this line after the Dim statement line

Code:
If MsgBox("Are you sure you want to clear the form?", vbYesNo + vbQuestion, "Warning...") <> vbYes Then Exit Sub
 
Upvote 0
Try this:
Code:
Sub ClearCells()
Dim rngLook As Range, rngC As Range, rngClear As Range
Set rngLook = ActiveSheet.UsedRange
For Each rngC In rngLook.Cells
    If rngC.Locked = False Then
        If rngClear Is Nothing Then
            Set rngClear = rngC
        Else
            Set rngClear = Union(rngClear, rngC)
        End If
    End If
Next rngC
 
If Not rngClear Is Nothing Then
    rngClear.ClearContents
End If
End Sub


Hi The code above doesn't delete the actual values that is in the unprotected cells.

It clears it it but when there is data it pulls through what was in there previously.

What i need is to delete anything in the unprotected/locked cells

Many Thanks

Any help from anyone is appreciated
 
Upvote 0
Hello,
Can you explain what you mean please?
It clears it it but when there is data it pulls through what was in there previously.
The code loops through all cells in the active sheet's used range, and combines all the unlocked cells into one range, then clears the contents from that combined range.

What do you mean by "unprotected/locked cells"? A cell is either locked or unlocked, and a worksheet is either protected or unprotected.
 
Upvote 0
Hello,
Can you explain what you mean please?

The code loops through all cells in the active sheet's used range, and combines all the unlocked cells into one range, then clears the contents from that combined range.

What do you mean by "unprotected/locked cells"? A cell is either locked or unlocked, and a worksheet is either protected or unprotected.

Hi,

I have data validation which where you can enter the number of teams etc...

Basically My worsheets are protected and cells are also locked other than certain cells where users can enter data/values (not locked). When the code runs it clears everything.

And leaves everything blank. Now say i select the number of teams. Certain cells where i have entered values come back through. i need it to delete all the values that was inputted in the unlocked cells.
 
Upvote 0
I'm sorry, I just don't understand what you're telling me.

You asked for code which deletes values in the cells which are not locked. This code does that.

If you want something different, please explain in single commands what you want, for example:

1. delete values in all cells which are not locked
2. do something else
3. if x, do y, otherwise do z

Regards
 
Upvote 0
I'm sorry, I just don't understand what you're telling me.

You asked for code which deletes values in the cells which are not locked. This code does that.

If you want something different, please explain in single commands what you want, for example:

1. delete values in all cells which are not locked
2. do something else
3. if x, do y, otherwise do z

Regards



Hi sorry if i'm not explaining myself properly

This is what i need
1. delete values in all cells which are not locked
 
Upvote 0

Forum statistics

Threads
1,224,544
Messages
6,179,430
Members
452,915
Latest member
hannnahheileen

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