Clearing multiple sheets VBA

Mr2017

Well-known Member
Joined
Nov 28, 2016
Messages
644
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi

The code below works with only one condition in the IF statement (the first one - Summary sheet).

However, when I add the other two sheet names that should NOT be cleared, it doesn't work?

Do I need to change something in the IF statement?

Thanks in advance.

Code that works:
Code:
Sub ClearSheets()
 
    Dim sht As Worksheet
    
    For Each sht In ThisWorkbook.Worksheets
    
        If sht.Name <> "Summary" Then
          sht.Cells.ClearContents
          
        End If
    Next


Exit Sub


End Sub

Code that doesn't work:

Code:
Sub ClearSheets()
 
    Dim sht As Worksheet
    
    For Each sht In ThisWorkbook.Worksheets
    
        If sht.Name <> "Summary" And sht.Name <> "Sheet2" And sht.Name <> "Sheet3" Then
          sht.Cells.ClearContents
          
        End If
    Next


Exit Sub


End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Maybe something like this ?

Code:
    Dim sht As Worksheet
    For Each sht In ThisWorkbook.Worksheets
    Select Case ws.Name
        Case "Summary", "Sheet2", "Sheet3"
            GoTo NEXTWS
        Case Else
            sht.Cells.ClearContents
    End Select
NEXTWS:
Next ws

Edit to add - I'm not a code expert, there may be better ways of doing this !
 
Last edited:
Upvote 0
Check that the actual sheet names are exactly the same as the names in your macro (e.g. any spaces?)
 
Last edited:
Upvote 0
Hi Gerald

This code gave me a Compile error: "Invalid Next Control variable reference"

Then it highlighted the WS in this line of the code (ie the second last line / just before the End Sub bit)

Next Ws

Do you know why that would be?
 
Upvote 0
Hi footoo

Yes, the sheet names are exactly the same as the names in the macro....
 
Upvote 0
Try this
Code:
Sub ClearSheets()
 
    Dim sht As Worksheet
    
    For Each sht In ThisWorkbook.Worksheets
    
        If sht.Name <> "Summary" And sht.Name <> "Sheet2" And sht.Name <> "Sheet3" Then
          Debug.Print "|" & sht.Name & "|"
          sht.Cells.ClearContents
          
        End If
    Next
End Sub
In the Immediate window it will print a list of sheet names that it's trying to clear. Copy & paste that list to the thread.
If the immediate window is not visible (normally below the code window) Ctrl G, will bring it up
 
Upvote 0
Hmmm....

Interesting!

That code worked!

The list of other sheets is quite long! Over 50! But it shows the sheets that it cleared, which is good!

And more importantly, didn't clear the sheets that I didn't want it to clear!

Thanks Fluff!

I must have made a typo!
 
Upvote 0
Glad it's working & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,252
Members
452,623
Latest member
Techenthusiast

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