Macro Help - Looping & Deleting

E

Excel Hates Me

Hi, could someone please help me with this macro? It only works on the
active sheet when the "if" condition is met; otherwise it deletes all
the columns to the right of column R. It does nothing to the other
sheets. Sorry, I'll get better at this eventually.

Thanks!

-----

Sub DeleteColumnsAll()

Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets

If InStr(Range("R2").Text, "Apple") Then
Columns("AC:AW").Delete
Else: Columns("S:AB").Delete
End If

Next ws

End Sub
 
J

James Ravenswood

Hi, could someone please help me with this macro? It only works on the
active sheet when the "if" condition is met; otherwise it deletes all
the columns to the right of column R. It does nothing to the other
sheets. Sorry, I'll get better at this eventually.

Thanks!

-----

Sub DeleteColumnsAll()

     Dim ws As Worksheet

     For Each ws In ActiveWorkbook.Worksheets

            If InStr(Range("R2").Text, "Apple") Then
            Columns("AC:AW").Delete
            Else: Columns("S:AB").Delete
            End If

     Next ws

End Sub

Actually, Excel loves you (it just has difficulty expressing that
love)
You need to qualify your references:

Sub ExcelLovesYou()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
With ws
If InStr(.Range("R2").Text, "Apple") Then
.Columns("AC:AW").Delete
Else
.Columns("S:AB").Delete
End If
End With
Next ws
End Sub
 
E

Excel Hates Me

Thank you James!

It does seem quite possible that I wouldn't understand Excel's
expressions anyway :)
 

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