ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Macro Help - Looping & Deleting (https://www.excelbanter.com/excel-programming/443498-macro-help-looping-deleting.html)

Excel Hates Me

Macro Help - Looping & Deleting
 
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

James Ravenswood

Macro Help - Looping & Deleting
 
On Aug 12, 7:22*pm, Excel Hates Me wrote:
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

Excel Hates Me

Macro Help - Looping & Deleting
 
Thank you James!

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


All times are GMT +1. The time now is 07:12 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com