Clear all worksheets EXCEPT ones I list

ItalianPlatinum

Well-known Member
Joined
Mar 23, 2017
Messages
793
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I am looking for a way to improve the way I am clearing my sheets by looping through and looking for maybe something faster. ie, clearing all worksheets in my workbook except 2 or 3 I list. Currently I have 20 worksheets

VBA Code:
' Run loop to clear sheets that would be in use
  i = 0
  Do Until WsCus.Range("FILTER").Offset(i, 0) = ""
    FILTER = WsCus.Range("FILTER").Offset(i, 0)
    SheetName = WsCus.Range("FILTER").Offset(i, -1).Value 'Assuming from your screenshot it's in the column left of the filter
    
    Worksheets(SheetName).Cells.ClearContents
    i = i + 1
Loop
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
If you have 2 or 3 that you want to exclude, you could just do something like:
VBA Code:
    Dim ws As Worksheet
    
    For Each ws In ActiveWorkbook.Worksheets
        If (ws.Name <> "Sheet1") And (ws.Name <> "Sheet2") And (ws.Name <> "Sheet3") Then
            ws.Cells.ClearContents
        End If
    Next ws

Where "Sheet1", "Sheet2", and "Sheet3" are the three we want to exclude.
 
Upvote 0
Will that ignore sheets I have hidden? if not then ill have more. The sheets I want to clear only have 1 character names
 
Upvote 0
Will that ignore sheets I have hidden?
That is an important detail you failed to mention!

If you want to also exclude hidden worksheets, use this version:
VBA Code:
    Dim ws As Worksheet
    
    For Each ws In ActiveWorkbook.Worksheets
        If (ws.Name <> "Sheet4") And (ws.Name <> "Sheet3") Then
            If ws.Visible = True Then ws.Cells.ClearContents
        End If
    Next ws
 
Upvote 0
Solution

Forum statistics

Threads
1,215,073
Messages
6,122,975
Members
449,095
Latest member
Mr Hughes

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