Error Checking in Excel
Thanks Thanks:  0
Likes Likes:  0
Results 1 to 3 of 3

Thread: Selecting a macro to perform in all workbooks

  1. #1
    New Member
    Join Date
    May 2002
    Location
    london
    Posts
    17
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    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

  2. #2
    Board Regular
    Join Date
    Mar 2002
    Location
    Cincinnati, Ohio, USA
    Posts
    6,824
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Maybe this will work. I did not test it.


    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



    Tom

  3. #3
    MrExcel MVP
    Join Date
    Feb 2002
    Location
    Sunny, spring-like Hull
    Posts
    3,339
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    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

Some videos you may like

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •