Deleting cells from multiple tabs

JustinCase

New Member
Joined
Jul 22, 2010
Messages
9
Hi all,

I'm looking at clearing all contents of a set cell range on multiple tabs

so was looking along the lines of ;


Worksheets(Array("ABCD", "UFDHD", "FGHDFHFD", "EWDHFGH")).Select
.Range("L2:P13").ClearContents

However i've missed a part out, and its driving me insane.

Is there also a better way to mark all worksheets as a varient so i can just call on for example worksheets(x)

Cheers.
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Welcome to the Board!

You can do something like this:

<font face=Calibri><SPAN style="color:#00007F">Sub</SPAN> Foo()<br>    <SPAN style="color:#00007F">Dim</SPAN> ws <SPAN style="color:#00007F">As</SPAN> Worksheet<br>        <br>        <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> ws <SPAN style="color:#00007F">In</SPAN> ActiveWorkbook.Worksheets<br>            ws.Range("A1").ClearContents<br>        <SPAN style="color:#00007F">Next</SPAN> ws<br>        <br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

If you need to exclude a sheet(s) you can add:

If ws.Name <> "SheetName" Then...

For your method remove the . before "Range".

HTH,
 
Last edited:
Upvote 0
Awesome, thanks for your help :)

Yeah having to do the second option mainly as there is about 50 sheets and i need to exclude 20 or so for this clear cell, and then need to have another range to clear for the other 20.

It's going to be a fun night im sure :)

Thanks!
 
Upvote 0
There are several ways to tackle it. For instance, you could build a list of sheets and the range(s) to be deleted on each then loop through the list/sheets.

But with limited selecting your recorded Array approach will probably be just fine.
 
Upvote 0
Hiya, Smitty!

Justin,

Some thoughts on thinking this thing through long term:

Get JKP's name manager addin so you can see (and edit) named items (ranges, formulas) that are invisible. Add a [hidden] name to each sheet (i.e. it's scope is the worksheet) whose value is "FALSE" or "TRUE" and your loop then tests that hidden name's value. This approach is MUCH more scaleable & maintainable. Plus if someone renames a sheet your code doesn't break.

Code:
Sub Foo()
    Const c_strHiddenName As String = "booClearSheet"
    Const c_strAddressToClear As String = "A1:B2"
    Dim ws As Worksheet, booClear As Boolean
 
    For Each ws In ActiveWorkbook.Worksheets
        Let booClear = False
        On Error Resume Next
        Let booClear = CBool(Replace(ws.Names(c_strHiddenName).Value, "=", ""))
        If booClear Then
            ws.Range(c_strAddressToClear).ClearContents
        End If
    Next ws
End Sub
Also, I would suggest using named ranges or at the very least defining a constant for the address for the cells that you want to clear. Again, makes things easier to maintain. Using a named range makes you robust against inserted rows or columns.
 
Last edited:
Upvote 0
Ah thanks for that suggestion Greg, i shall look into it in the morning (should probably stop now its 1:30am :) )
 
Upvote 0
Oh i've just realised ;


Sub ClearCell()

Worksheets(Array("asdfsafsd", "fghfghfghgf", "hjkhjkhjkhk", "dhfhfhfhfgh")).Select
Range("L2:P13").ClearContents

End Sub


Seems to only work for the first worksheet and doesn't cycle through them. I'm sure it should cycle as it works for a multiple v lookup that i was using.
 
Upvote 0
Oh i've just realised ;


Sub ClearCell()

Worksheets(Array("asdfsafsd", "fghfghfghgf", "hjkhjkhjkhk", "dhfhfhfhfgh")).Select
Range("L2:P13").ClearContents

End Sub


Seems to only work for the first worksheet and doesn't cycle through them. I'm sure it should cycle as it works for a multiple v lookup that i was using.

Yeah, you'd think it would, wouldn't you. Try this.

Code:
Sub foo()
    x = Array("Sheet2", "Sheet3")
    Sheets(x(0)).Select
    Range("B1:C4").ClearContents
    Sheets(x).FillAcrossSheets Worksheets(x(0)).Range("B1:C4")
 
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,551
Messages
6,114,273
Members
448,559
Latest member
MrPJ_Harper

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