Selecting a macro to perform in all workbooks

Bulsara

New Member
Joined
May 9, 2002
Messages
17
Hello

Does any one know how to get a macro to perform a task on all worksheets in a workbook without having to specify each sheet individualy.i.e I have a macro that deletes a row based on a certain criteria and I want it to run in all the worksheets in a workbook.

Also is there an esay way of specifiying a macro to delete all rows where dates in a specific row/cell are older than todays? I have messed around with now and today function and came up with the following but it's a bit long winded.

The way I have done it currently is:

Sub Deleterows()
Dim i As Double
Worksheets("Text").Cells(20, 1) = "=today()" ' INSERTS CURRENT DATE IN THAT CELL

Worksheets("non").Select
For i = 100 To 1 Step -1
If Cells(i, 1).Value < Worksheets("Text").Cells(20, 1) Then _
Cells(i, 1).EntireRow.Delete
Next i

Thanks very much.

Hamnet
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Maybe this will work. I did not test it.

<pre>
Sub Deleterows()
Dim i As Integer, sh As Worksheet
For Each sh In Worksheets
For i = 100 To 1 Step -1
If Not DateDiff("d", sh.Cells(i, 1).Value, Now) = 0 Then _
sh.Cells(i, 1).EntireRow.Delete
Next i
Next
End Sub

</pre>

Tom
 
Upvote 0
Just a couple of additions Tom: -

Sub Deleterows()
Dim i As Integer, sh As Worksheet
Application.ScreenUpdating = False
For Each sh In Worksheets
For i = 100 To 1 Step -1
If IsDate(sh.Cells(i, 1)) Then
If Not DateDiff("d", sh.Cells(i, 1).Value, Now) = 0 Then _
sh.Cells(i, 1).EntireRow.Delete
End If
Next i
Next sh
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,527
Messages
6,114,140
Members
448,551
Latest member
Sienna de Souza

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