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.
 

Some videos you may like

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

Smitty

Legend
Joined
May 15, 2003
Messages
29,536
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:

JustinCase

New Member
Joined
Jul 22, 2010
Messages
9
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!
 

Smitty

Legend
Joined
May 15, 2003
Messages
29,536
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.
 

Greg Truby

MrExcel MVP
Joined
Jun 19, 2002
Messages
10,014

ADVERTISEMENT

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:

JustinCase

New Member
Joined
Jul 22, 2010
Messages
9
Ah thanks for that suggestion Greg, i shall look into it in the morning (should probably stop now its 1:30am :) )
 

JustinCase

New Member
Joined
Jul 22, 2010
Messages
9

ADVERTISEMENT

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.
 

Greg Truby

MrExcel MVP
Joined
Jun 19, 2002
Messages
10,014
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
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,106,712
Messages
5,512,990
Members
408,928
Latest member
Black Vinyl

This Week's Hot Topics

  • Sort code advice please
    Hi, I have the code below which im trying to edit but getting a little stuck. This was the original code which worked fine,columns A-F would sort...
  • SUMPRODUCT with nested If statement
    Hi everyone, Hope you're all well. I'm hoping someone will be able to point me in the right direction with a problem I'm having with a SUMPRODUCT...
  • VBA - simple sort is killing me!
    Hello all! This should be so easy, but not for me, apparently! I have a table of data that can be of varying lengths and widths. My current macro...
  • Compare Two Lists
    I have two Lists and I need to be able to Identify differences between them. List 100 comes from a workbook - the other is downloaded form the...
  • Formula that deducts points for each code I input.
    I am trying to create a formula that will have each student in my class start at 100 points and then for each code that I enter (PP for Poor...
  • Conditional formatting formula required for day of week and a value
    Hi, I have a really simple spreadsheet where column A is the date, column B is the activity total shown as a number and column C states the day of...
Top