Delete a range on all worksheets but 1

Juenapa

New Member
Joined
Mar 4, 2011
Messages
5
Hi,

I'm trying very hard to Delete a range on all worksheets but 1.

I have a code that protects all sheets and I'm trying to edit it to delete a range on all worksheets but 1. I've changed the "if" line to read:

If objSheet.Sheet2.Name = True Then objSheet.Cells.Range("a1:j9").ClearContents

Where am I going wrong? Or should I being going a different way.

Thanks


Working code

"Public Sub aProtectAllSheets()

Dim objSheet As Worksheet

'Protecting all worksheets with password
For Each objSheet In Worksheets
If objSheet.ProtectContents = False Then objSheet.Protect "abc"
Next objSheet

End Sub"
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Hi Juenapa,

try something like this:

Code:
Option Explicit
 
Sub ClearA1toJ9()
 
Dim ws As Worksheet
Dim TheName As String
 
TheName = Application.InputBox("Type name of sheet NOT to clear cells A1:J9 in", , "Sheet2")
For Each ws In ActiveWorkbook.Worksheets
    If Not ws.Name = TheName Then
        ws.Range("A1:J9").ClearContents
    End If
Next ws
 
End Sub
 
Upvote 0
I now have this code that is working, just need to omit a sheet from doing all sheets.
Was thinking maybe something like "If Not".

Sub ClearAll()
Dim Sh As Worksheet
On Error Resume Next
For Each Sh In Worksheets
Sh.Range("a1:J34").ClearContents
Next
End Sub
 
Upvote 0
Try

Code:
If Sh.Name <> "Sheet Name to Omit" Then
    Sh.Range("a1:J34").ClearContents
End If
 
Upvote 0
hstaubyn option would great, changed it slightly to get rid of the message box.

Just need to be able to do 5 pages in stead of 1.

Thanks

This works:

Option Explicit

Sub ClearA1toJ92()

Dim ws As Worksheet
Dim TheName As String

TheName = "Sheet2"
For Each ws In ActiveWorkbook.Worksheets
If Not ws.Name = TheName Then
ws.Range("A1:J9").ClearContents
End If
Next ws

End Sub
 
Upvote 0
I now have this (See Below), which works. But I now need to do the same but instead of ClearContents I need to unlock cells.

The vba code when recording a new macro is:
Selection.Locked = False
Selection.FormulaHidden = False


Thanks

Jason


Option Explicit

Sub exam()
Dim wks As Worksheet

For Each wks In ThisWorkbook.Worksheets
Select Case wks.Name
Case "Sheet3", "Sheet4", "Sheet5", "Sheet6", "Sheet7"
Case Else
wks.Range("A1:J9").ClearContents
End Select
Next
End Sub
 
Upvote 0
Just change it slightly and it works fine.

Thanks

Sub AA_Unlock_Cells()
Dim wks As Worksheet

For Each wks In ThisWorkbook.Worksheets
Select Case wks.Name
Case "Home", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"
Case Else
wks.Range("b2:f6").Locked = False

End Select
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,585
Messages
6,179,702
Members
452,938
Latest member
babeneker

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