Worksheets Names array with For Each

ilikered100

New Member
Joined
May 29, 2013
Messages
46
I'm sure I am missing something very simple, but I cannot get this code to work properly.

For the purpose of this example, let's say I have a workbook with 10 worksheets, each worksheet named with a different color. I want to use an array of three worksheet names ("Red", "Blue" , "Green") and clear the contents of only those three sheets. When I run the code below, whatever worksheet I'm on (whether in the array or not), the code runs three times on that sheet only. It only clears the content on the worksheet I am currently on.... again whether that worksheet is one of the three in the array or not. I've tried many variations of the below without success.

Sub ClearSpecificSheets()​

Dim ws As Worksheet​
Dim arrListofSheets As Variant​

Set arrListofSheets = ActiveWorkbook.Sheets(Array("Red", "Blue", "Green"))​

For Each ws In arrListofSheets​
Cells.Select​
Selection.ClearContents​
Next ws​


End Sub

Any help is appreciated.

Carolyn
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Its because you arent qualifying the Cells with a sheet so by default excel will use the activesheet. Try:

Code:
For Each ws In arrListofSheets
    ws.Cells.ClearContents
Next ws
 
Upvote 0
Thank you - It worked! I thought I had tried that. I would tell you how long I spent trying to get that this code to work, but I'd probably be kicked out of the Forum!

Thank you again!

Uggghhh!

Carolyn
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,249
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