+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    03-09-2010
    Location
    Dublin, Ireland
    MS-Off Ver
    Excel 2007
    Posts
    11

    apply macro to all worksheets in the workbook

    Hi,

    I cannot figure out why this macro only runs on the active worksheet and not all worksheets in the workbook. The macro is to delete rows with a zero value in the end colum (in this case colum G). Can anyone advise? thanks

    Code:
    Sub Delete_zero_Rows()
        Application.ScreenUpdating = False
       Application.Calculation = xlCalculationManual
       Dim ws As Worksheet
        For Each ws In ActiveWorkbook.Worksheets
       Dim lastrow As Long, r As Long
       lastrow = ws.UsedRange.Rows.Count
       For r = lastrow To 1 Step -1
        If UCase(Cells(r, 7).Value) = "0" Then Rows(r).Delete
        Next r
        Next ws
       Application.Calculation = xlCalculationAutomatic
       Application.ScreenUpdating = True
    End Sub
    Last edited by jbyrne; 03-15-2010 at 10:40 AM.

  2. #2
    Forum Moderator romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    Alibi
    MS-Off Ver
    All
    Posts
    8,262

    Re: apply macro to all worksheets in the workbook

    Because this line:
    Code:
        If UCase(Cells(r, 7).Value) = "0" Then Rows(r).Delete
    needs to be this:
    Code:
        If UCase(ws.Cells(r, 7).Value) = "0" Then ws.Rows(r).Delete
    So long, and thanks for all the fish.

  3. #3
    Registered User
    Join Date
    03-09-2010
    Location
    Dublin, Ireland
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: apply macro to all worksheets in the workbook

    thanks romperstomper. that did the trick

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0