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
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
I can delete rows with "No" in but im after deleting columns instead, this is what I have for rows but cant convert it to work with columns, any help guys.

Code:
Sub test()
With ActiveSheet
    .AutoFilterMode = False
    With Range("C2", Range("C" & rows.Count).End(xlUp))
        .AutoFilter 1, "*No*"
        On Error Resume Next
        .Offset(1).SpecialCells(12).EntireRow.Delete
    End With
    .AutoFilterMode = False
End With
End Sub
 
Last edited:
Upvote 0
Try this:
Code:
Sub Delete_Columns()
Application.ScreenUpdating = False
Dim c As Range
    For Each c In Range("AM2:C2")
        If c.Value = "No" Then c.Columns.Delete
    Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi M.A.I.T do you get any issues if you run your code with the set up below?


Excel 2010
CDEFGHI
1
2NoNoNoNoNoNoNo
Sheet1
 
Upvote 0
Hi My answer Is This,

The code is nearly there it deletes the cells with "No" in but not the entire column
 
Upvote 0
Sorry missed that.
Try this:
Code:
Sub Delete_Columns()
Application.ScreenUpdating = False
Dim c As Range
    For Each c In Range("AM2:C2")
        If c.Value = "No" Then c.EntireColumn.Delete
    Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
I still think there is an issue with the layout in post # 4.
 
Upvote 0
That works perfect now, but for some reason i need to run the code twice to get all of the columns, I have never seen that before, any ideas?
 
Upvote 0
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
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


Mark858 I see what you mean now, your code does it perfect. Thank you so much for preserving with this :).
Top banana mate.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,248
Members
452,623
Latest member
cliftonhandyman

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