+ Reply to Thread
Results 1 to 5 of 5

Loop that deletes sheets if data ends on a certain row

  1. #1
    Please Help
    Guest

    Loop that deletes sheets if data ends on a certain row

    Hi everyone,

    I need to have a loop that goes through all of the sheets I've created as
    part of my macro output and deletes the sheets that contain no information.
    These sheets will all have data that will end on row #7. I'm having problems
    with this because when you delete a sheet it seems to change the reference
    point of the active sheet and the next thing you know i'm getting a
    "subscript out of range" error.

    I've tried to do this by using a FOR loop that loops thru each sheet and if
    the last row of data is a 7, deletes the sheet...no success. The total
    number of sheets will always be 15. It looks something like this:

    For count = 1 to 15

    lastrow = (lastrow finder code here)
    If lastrow = 7 then
    activesheet.delete
    end if

    next count

    Please let me know your suggestions for solving this problem. Thanks in
    advance for your help!





  2. #2
    Tom Ogilvy
    Guest

    Re: Loop that deletes sheets if data ends on a certain row

    For count = 15 to 1 step -1

    lastrow = (lastrow finder code here)
    If lastrow = 7 then
    activesheet.delete
    end if

    next count

    --
    Regards,
    Tom Ogilvy

    "Please Help" <[email protected]> wrote in message
    news:[email protected]...
    > Hi everyone,
    >
    > I need to have a loop that goes through all of the sheets I've created as
    > part of my macro output and deletes the sheets that contain no

    information.
    > These sheets will all have data that will end on row #7. I'm having

    problems
    > with this because when you delete a sheet it seems to change the reference
    > point of the active sheet and the next thing you know i'm getting a
    > "subscript out of range" error.
    >
    > I've tried to do this by using a FOR loop that loops thru each sheet and

    if
    > the last row of data is a 7, deletes the sheet...no success. The total
    > number of sheets will always be 15. It looks something like this:
    >
    > For count = 1 to 15
    >
    > lastrow = (lastrow finder code here)
    > If lastrow = 7 then
    > activesheet.delete
    > end if
    >
    > next count
    >
    > Please let me know your suggestions for solving this problem. Thanks in
    > advance for your help!
    >
    >
    >
    >




  3. #3
    Markus Scheible
    Guest

    Loop that deletes sheets if data ends on a certain row

    Hi unknown,

    could you please use your real name?


    >For count = 1 to 15
    >
    >lastrow = (lastrow finder code here)
    >If lastrow = 7 then
    >activesheet.delete


    use sheets(count).delete instead


    Best

    Markus


    >end if
    >
    >next count
    >
    >Please let me know your suggestions for solving this

    problem. Thanks in
    >advance for your help!
    >
    >
    >
    >
    >.
    >


  4. #4
    K Dales
    Guest

    RE: Loop that deletes sheets if data ends on a certain row

    The best way to loop and delete is with a For Each... Next loop; e.g.

    Dim ThisSheet as Worksheet

    For Each ThisSheet in ThisWorkbook.Worksheets
    If (lastrow finder code) then ThisSheet.delete
    Next ThisSheet


    "Please Help" wrote:

    > Hi everyone,
    >
    > I need to have a loop that goes through all of the sheets I've created as
    > part of my macro output and deletes the sheets that contain no information.
    > These sheets will all have data that will end on row #7. I'm having problems
    > with this because when you delete a sheet it seems to change the reference
    > point of the active sheet and the next thing you know i'm getting a
    > "subscript out of range" error.
    >
    > I've tried to do this by using a FOR loop that loops thru each sheet and if
    > the last row of data is a 7, deletes the sheet...no success. The total
    > number of sheets will always be 15. It looks something like this:
    >
    > For count = 1 to 15
    >
    > lastrow = (lastrow finder code here)
    > If lastrow = 7 then
    > activesheet.delete
    > end if
    >
    > next count
    >
    > Please let me know your suggestions for solving this problem. Thanks in
    > advance for your help!
    >
    >
    >
    >


  5. #5
    Please Help
    Guest

    RE: Loop that deletes sheets if data ends on a certain row

    Thanks To All for your help.. that was fast and it works great!!! I used
    Tom's suggestion since it was the easiest change for my code.

    Thanks!
    Curtis

    "Please Help" wrote:

    > Hi everyone,
    >
    > I need to have a loop that goes through all of the sheets I've created as
    > part of my macro output and deletes the sheets that contain no information.
    > These sheets will all have data that will end on row #7. I'm having problems
    > with this because when you delete a sheet it seems to change the reference
    > point of the active sheet and the next thing you know i'm getting a
    > "subscript out of range" error.
    >
    > I've tried to do this by using a FOR loop that loops thru each sheet and if
    > the last row of data is a 7, deletes the sheet...no success. The total
    > number of sheets will always be 15. It looks something like this:
    >
    > For count = 1 to 15
    >
    > lastrow = (lastrow finder code here)
    > If lastrow = 7 then
    > activesheet.delete
    > end if
    >
    > next count
    >
    > Please let me know your suggestions for solving this problem. Thanks in
    > advance for your help!
    >
    >
    >
    >


+ 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