+ Reply to Thread
Results 1 to 5 of 5

Unable to Loop Multiple Worksheets

  1. #1
    jackc
    Guest

    Unable to Loop Multiple Worksheets

    I have a macro that deletes unwanted rows from a worksheet. The macro
    works fine when applied to each worksheet individually, when I attempt
    to loop in all worksheets it only deletes the rows in the first
    worksheet.

    Sub Delete_Rows()
    ' This macro deletes specified rows on the active worksheet

    Dim rng As Range, cell As Range, del As Range

    For Each ws In ActiveWorkbook.Worksheets
    ws.Activate
    If ws.Name <> "Summary" And ws.Name <> "Reference" Then
    Set rng = Intersect(Range("C6:C600"), ActiveSheet.UsedRange)
    For Each cell In rng
    If (cell.Value) = "USD" _
    Or (cell.Value) = "USD Total" _
    Or (cell.Value) = "KEY" _
    Or (cell.Value) = "EUR" Then
    If del Is Nothing Then
    Set del = cell
    Else: Set del = Union(del, cell)
    End If
    End If
    Next
    On Error Resume Next
    del.EntireRow.Delete
    End If
    Next
    End Sub

    Does anyone have a solution.


  2. #2
    Registered User
    Join Date
    12-02-2005
    Posts
    16
    I copied your code to a new workbook and inserted USD in random cells on three sheets. Your code went to all three sheets and removed the USD. Is your code actually going to all the sheets you need it to go to or is there some other problem. From the wording of your problem I am assuming that the For statement is not activating the other sheets. Is this correct?
    JAVB

  3. #3
    jackc
    Guest

    Re: Unable to Loop Multiple Worksheets

    The macro seems to be looping thru each of the sheets. However, it is
    only deleting the rows in the first sheet.

    When you say you entered USD in random cells, I assume they were in
    column "C" after row 5.


  4. #4
    Dave Peterson
    Guest

    Re: Unable to Loop Multiple Worksheets

    A range belongs to a worksheet. When you loop through the first worksheet, you
    build that del range up with cells you need.

    When you go to the next worksheet, del is never re-initialized to nothing. And
    since del has been deleted, it's kind of in a wierd situation. It isn't an
    existing range--it's kind of in a netherworld (my word--not anyone elses!)

    You can just make sure that del is reset to Nothing each time you start
    processing a different worksheet:

    For Each ws In ActiveWorkbook.Worksheets
    ws.Activate
    set del = nothing



    jackc wrote:
    >
    > I have a macro that deletes unwanted rows from a worksheet. The macro
    > works fine when applied to each worksheet individually, when I attempt
    > to loop in all worksheets it only deletes the rows in the first
    > worksheet.
    >
    > Sub Delete_Rows()
    > ' This macro deletes specified rows on the active worksheet
    >
    > Dim rng As Range, cell As Range, del As Range
    >
    > For Each ws In ActiveWorkbook.Worksheets
    > ws.Activate
    > If ws.Name <> "Summary" And ws.Name <> "Reference" Then
    > Set rng = Intersect(Range("C6:C600"), ActiveSheet.UsedRange)
    > For Each cell In rng
    > If (cell.Value) = "USD" _
    > Or (cell.Value) = "USD Total" _
    > Or (cell.Value) = "KEY" _
    > Or (cell.Value) = "EUR" Then
    > If del Is Nothing Then
    > Set del = cell
    > Else: Set del = Union(del, cell)
    > End If
    > End If
    > Next
    > On Error Resume Next
    > del.EntireRow.Delete
    > End If
    > Next
    > End Sub
    >
    > Does anyone have a solution.


    --

    Dave Peterson

  5. #5
    jackc
    Guest

    Re: Unable to Loop Multiple Worksheets

    Dave,

    Thanks a lot, I'm sure this will work

    Jack C


+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.6.0 RC 1