How to run same code on all sheets

DaveUK

Board Regular
Joined
Jan 24, 2005
Messages
245
Please could somebody tell me how to programatically do the following.

The code i have is this

Code:
 Cells.ClearContents

Instead of selecting all worksheets in my workbook by name how do i go about selecting all worksheets in my workbook and running the above code en mass?

TIA
 

Some videos you may like

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

SydneyGeek

MrExcel MVP
Joined
Aug 5, 2003
Messages
12,251
Hi Dave,

If you really want to clear out all cells, try this:
Code:
Sub ClearAllCells()
    Dim Sht As Worksheet
    
    For Each Sht In ActiveWorkbook.Sheets
        Sht.UsedRange.ClearContents
    Next Sht
        
End Sub
Denis
 

Watch MrExcel Video

Forum statistics

Threads
1,118,530
Messages
5,572,697
Members
412,481
Latest member
nhantam
Top