Looping worksheets in workbook

G

Guest

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.
 
B

Bob Phillips

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)
 
T

Tushar Mehta

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top