Delete only Columns containing "No"

berty2000

Board Regular
Joined
Mar 29, 2011
Messages
71
In VBA is it possible to delete the whole columns containing the word "No" in the range of C2:AM2 ?

Thanks in advance
 
On My limited test my script worked but when all cells in row 2 had "No" it missed some. I figured working the range backwards would allow it to work. Live and learn. I do know about running scripts backwards in some cases.
That's why I was querying it... try the code below

Code:
Sub Delete_Columns()
Application.ScreenUpdating = False
Dim x As Long
    For x = 39 To 3 Step -1
        If Cells(2, x).Value = "No" Then Cells(2, x).EntireColumn.Delete
    Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Hi M.A.I.T. the trouble you were having is that when using the range method Excel always works from the lowest cell to the highest cell so when you wrote
Code:
Range("AM2:C2")
Excel (being helpful?) doesn't treat it as an error or work backwards, it interprets it as
Code:
Range("C2:AM2")
Then you get the usual Shift problem (similar to what you get when deleting rows).

See the order produced by the code below

Code:
Sub Demo()

Dim c As Range
    For Each c In Range("AM2:C2")
       Debug.Print c.Column
    Next

End Sub
 
Last edited:
Upvote 0
Yep. I know that now. Thanks.
Hi M.A.I.T. the trouble you were having is that when using the range method Excel always works from the lowest cell to the highest cell so when you wrote
Code:
Range("AM2:C2")
Excel (being helpful?) doesn't treat it as an error or work backwards, it interprets it as
Code:
Range("C2:AM2")
Then you get the usual Shift problem (similar to what you get when deleting rows).

See the order produced by the code below

Code:
Sub Demo()

Dim c As Range
    For Each c In Range("AM2:C2")
       Debug.Print c.Column
    Next

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top