code will only clear one worksheet I need to have it clear multiple sheets within the range given.

ShawnSPS

Board Regular
Joined
Mar 15, 2023
Messages
60
Office Version
  1. 2003 or older
Platform
  1. Windows
Sub ClearContents()
Dim iterator As Long
Application.DisplayAlerts = False
For iterator = ThisWorkbook.Worksheets.Count To 1 Step -1
With ThisWorkbook.Worksheets(iterator)
If .CodeName <> "Znotes,ZShortcuts" Then Range("D3:AH31,D34:AH41").ClearContents

End With
Next iterator
Application.DisplayAlerts = True
End Sub
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
You need to correctly reference the sheet's range since you aren't selecting or activating the sheet to be cleared:
If .CodeName <> "Znotes,ZShortcuts" Then .Range("D3:AH31,D34:AH41").ClearContents

Ps. by the way, is "Znotes,ZShortcuts" a sigle sheet name or should they be two sheets.
 
Upvote 0
You need to correctly reference the sheet's range since you aren't selecting or activating the sheet to be cleared:
If .CodeName <> "Znotes,ZShortcuts" Then .Range("D3:AH31,D34:AH41").ClearContents

Ps. by the way, is "Znotes,ZShortcuts" a sigle sheet name or should they be two sheets.
Znotes and Zshortcuts are two seperate sheets, that I am trying try bypass as the code clears of the contents of the other sheets via the range I gave.
 
Upvote 0
Use this test instead:
If Not (.CodeName = "Znotes" Or .CodeName = "ZShortcuts") Then .Range("D3:AH31,D34:AH41").ClearContents
 
Upvote 0
Use this test instead:
If Not (.CodeName = "Znotes" Or .CodeName = "ZShortcuts") Then .Range("D3:AH31,D34:AH41").ClearContents
I tried your code, it failed only because Znote and Zshortcuts were protected. once I unprotected the sheets with your code and what I added it works flawless now.

Sub ClearContents()
Dim iterator As Long
Application.DisplayAlerts = False
For iterator = ThisWorkbook.Worksheets.Count To 1 Step -1
With ThisWorkbook.Worksheets(iterator)
'If .CodeName <> "Znotes,ZShortcuts" Then Range("D3:AH31,D34:AH41").ClearContents
Worksheets("Znotes").Unprotect ("Pila1DA.#")
Worksheets("ZShortCuts").Unprotect ("Pila1DA.#")
If Not (.CodeName = "Znotes" Or .CodeName = "ZShortcuts") Then .Range("D3:AH31,D34:AH41").ClearContents

End With
Next iterator
Application.DisplayAlerts = True
End Sub
 
Upvote 0
It works even with protected sheets. Are you sure you got the exact codenames (ex. capital letters) ? they need to exactly match (strings in macro / sheet's codename).

To avoid issues you could use:
If Not (UCase(.CodeName) = UCase("ZNotes") Or UCase(.CodeName) = UCase("ZShortcuts")) Then .Range("D3:AH31,D34:AH41").ClearContents
so whatever you write, the comparison will be between all capital letters.
 
Last edited:
Upvote 0
It works even with protected sheets. Are you sure you got the exact codenames (ex. capital letters) ? they need to exactly match (strings in macro / sheet's codename).

To avoid issues you could use:
If Not (UCase(.CodeName) = UCase("ZNotes") Or UCase(.CodeName) = UCase("ZShortcuts")) Then .Range("D3:AH31,D34:AH41").ClearContents
so whatever you write, the comparison will be between all capital letters.
yes I used Worksheets("Znotes").Unprotect ("Pila1DA.#") and Worksheets("ZShortCuts").Unprotect ("Pila1DA.#") in another macro so the spelling is correct and works perfectly. so with what I have currently now it is working as it should it clears all data in the range I gave except Znotes and ZShortcuts but that only happens if I unprotect those sheets when it is executed.
 
Upvote 0
Solution
Sorry, can't replicate your issue with protected sheets. The only way to error is when sheet codename (or if using sheetname) doesn't coincide.
Anyway, if it's okay for you, it's okay for me(y). Glad having been of some help ;).
 
Upvote 0

Forum statistics

Threads
1,215,361
Messages
6,124,500
Members
449,166
Latest member
hokjock

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