Code that errase

RAM

Well-known Member
Joined
Oct 4, 2004
Messages
1,862
Hi guys,

I could need a code that errase all unlocked cells. The code must work in grouped sheets.

Thanks,

RAM
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Without installing some customised add-ins I don't think you can do this without iterating through the array of sheets and used cells and checking protected status of each.... ie no way of creating an array of protected cells using built in libraries.

Code:
Sub x()
Dim ws As Worksheet
Dim rng As Range
Dim cell As Range
For Each ws In Worksheets(Array("Sheet1", "Sheet2"))
    With ws
        Set rng = .UsedRange
        For Each cell In rng
            Select Case cell.Locked
                Case False
                    cell.Value = ""
            End Select
        Next cell
    End With
Next ws
End Sub
 
Upvote 0
sorry, just realised you probably won't know the array until run-time in which case instead of

Code:
For Each ws In Worksheets(Array("Sheet1", "Sheet2"))

use

Code:
For Each ws In ActiveWindow.SelectedSheets
 
Upvote 0
Thanks a bundle,

There is only going to be one problem. I'm using this code to name the sheet, i.e the sheet name can vary:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Target = "" Then Exit Sub
If Target.Address <> "$AD$3" Then Exit Sub
ActiveSheet.Name = Range("AD3").Text
End Sub

Your code must not run a certain, named,set of sheets (even though it would be great, it doesn't work this time), but it must run when I group sheets (just a added infomation). It must run on the selected (active) sheets, NOT on any others.

Thanks,
RAM
 
Upvote 0
Just to avoid any errors, let's add a msg box: "Are you sure you want to remove all data?" YES-NO

RAM
 
Upvote 0
Code:
Sub x()
Dim ws As Worksheet
Dim rng As Range
Dim cell As Range
If MsgBox("Delete All Data?",vbYesNo,"Run Clear?") = vbNo Then Exit Sub
For Each ws In ActiveWindow.SelectedSheets
    With ws
        Set rng = .UsedRange
        For Each cell In rng
            Select Case cell.Locked
                Case False
                    cell.Value = ""
            End Select
        Next cell
    End With
Next ws
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,965
Messages
6,122,499
Members
449,089
Latest member
Raviguru

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