Trying to put a loop in a macro to clear certain worksheets

sspatriots

Well-known Member
Joined
Nov 22, 2011
Messages
585
Office Version
  1. 365
Platform
  1. Windows
As the thread title suggest, I cannot get a small piece of code to loop through some specific worksheets in a workbook to delete the entire contents on each worksheet (including tables). Below is what I have, but no matter what I do to it, it doesn't loop to the next worksheet. I'm guessing there is some simple principle that I'm missing here, but can't find it. Any help is much appreciated. Thanks, Steve

VBA Code:
Dim Sht As Variant
Dim Shts As Variant

Shts = Array("2023", "2022", "2021", "2020", "2019")

For Each Sht In Shts    'Loop Sheets Code goes here:
    Cells.Select
    Selection.ClearContents
Next Sht
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Try something like the following:

VBA Code:
    Dim Sht As Variant
    Dim Shts As Variant
'
    Shts = Array("2023", "2022", "2021", "2020", "2019")
'
    For Each Sht In Shts    'Loop Sheets Code goes here:
        On Error Resume Next
            Sheets(Sht).Cells.ClearContents
        On Error GoTo 0
    Next
 
Upvote 0
Or possibly...
VBA Code:
Dim ws As Worksheet
For Each ws In Sheets(Array("2023", "2022", "2021", "2020", "2019"))
    ws.Cells.ClearContents
Next ws
 
Upvote 0
LTNS kelvin. :) What happens if the sheet name in the array doesn't exist?
 
Upvote 0
But now that you have me look at the code that I originally supplied, I shorten my answer to:

VBA Code:
    Dim Sht As Variant
'
    For Each Sht In Array("2023", "2022", "2021", "2020", "2019")    'Loop Sheets Code goes here:
        On Error Resume Next
            Sheets(Sht).Cells.ClearContents
        On Error GoTo 0
    Next
 
Upvote 0
Solution
1689227802446.png
 
Upvote 0
Another option:

VBA Code:
Option Explicit
Sub Test()

    Dim ar As Variant, i As Long
    ar = Array("2023", "2022", "2021", "2020", "2019")
    
    For i = 0 To UBound(ar)
          On Error Resume Next '---->Just in case a sheet no longer exists.
               Sheets(ar(i)).Cells.Clear
          On Error GoTo 0
    Next i

End Sub

I hope that this helps.
Cheerio,
vcoolio.
 
Upvote 0
But now that you have me look at the code that I originally supplied, I shorten my answer to:

VBA Code:
    Dim Sht As Variant
'
    For Each Sht In Array("2023", "2022", "2021", "2020", "2019")    'Loop Sheets Code goes here:
        On Error Resume Next
            Sheets(Sht).Cells.ClearContents
        On Error GoTo 0
    Next
I ended up using this code because it was much like other code I had elsewhere in my workbook. I just couldn't figure out how to make it work. Thank you.
 
Upvote 0
Thanks for all the suggestions. I ended up going with the solution in Post #5 because of the similarities with some code I already had. Thanks again, SS
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,021
Members
452,374
Latest member
keccles

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