+ Reply to Thread
Results 1 to 3 of 3

Looping worksheets in workbook

  1. #1
    Wylie C
    Guest

    Looping worksheets in workbook

    I need help with a macro that will loop through all worksheets in a workbook
    and delete the worksheet if the contents in A15 <> "C" then re-loop and
    delete the "C" in each workbook. Should I create a named range using A15 in
    each workbook or just the cell reference as above?

    Thank you.

  2. #2
    Bob Phillips
    Guest

    Re: Looping worksheets in workbook


    Application.DisplayAlerts = False
    For Each sh In Activeworkbook.Worksheets
    If sh.Range("A15").Value <> "C" Then
    sh.Delete
    Else
    sh.Range("A15").Value = "C"
    End If
    Next sh

    --

    HTH

    RP
    (remove nothere from the email address if mailing direct)


    "Wylie C" <[email protected]> wrote in message
    news:[email protected]...
    > I need help with a macro that will loop through all worksheets in a

    workbook
    > and delete the worksheet if the contents in A15 <> "C" then re-loop and
    > delete the "C" in each workbook. Should I create a named range using A15

    in
    > each workbook or just the cell reference as above?
    >
    > Thank you.




  3. #3
    Tushar Mehta
    Guest

    Re: Looping worksheets in workbook

    The code below is untested. Like Bob's it uses a single pass through
    the workbook but differs on what it does with non-deleted worksheets.

    Option Explicit

    Sub testIt()
    Dim aWS As Worksheet
    For Each aWS In ActiveWorkbook.Worksheets
    With aWS.Range("A15")
    If .Value <> "C" Then
    .Parent.Delete
    Else
    .ClearContents
    End If
    Next aWS
    End Sub

    --
    Regards,

    Tushar Mehta
    www.tushar-mehta.com
    Excel, PowerPoint, and VBA add-ins, tutorials
    Custom MS Office productivity solutions

    In article <[email protected]>,
    [email protected] says...
    > I need help with a macro that will loop through all worksheets in a workbook
    > and delete the worksheet if the contents in A15 <> "C" then re-loop and
    > delete the "C" in each workbook. Should I create a named range using A15 in
    > each workbook or just the cell reference as above?
    >
    > Thank you.
    >


+ 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