Delete entire column based on cell value

rabbits

New Member
Joined
Aug 24, 2022
Messages
10
Office Version
  1. 365
Platform
  1. Windows
I have a large workbook with multiple columns. In row 9 I have labels that are either 'REMOVE', 'CHECK' or 'INSTALL'. I'm trying to write a macro that deletes all columns with 'remove'. The number of remove columns varies every time.

I've adapted this macro from something I found online, it sort-of works but seems to only delete every other remove column. Could someone tell me where I'm going wrong?

Rich (BB code):
Sub Delete_Column()
Dim cell As Range
For Each cell In Range("9:9")
    If cell.Value = "REMOVE" Then
        cell.EntireColumn.Delete
    End If
Next cell
End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Welcome to the Board!

The issue is that when deleting columns, the other columns shift over to the left, into the range you already checked.
So you need to loop through the column backwards, like this:
VBA Code:
Sub Delete_Column()

    Dim lc As Long
    Dim c As Long
    
'   Find last column used in row 9
    lc = Cells(9, Columns.Count).End(xlToLeft).Column
    
'   Loop through all columns backwards
    For c = lc To 1 Step -1
        If Cells(9, c).Value = "REMOVE" Then Cells(9, c).EntireColumn.Delete
    Next c
    
End Sub
 
Upvote 0
Solution
Thanks for this - worked like a dream.

Just as a follow-up, how would I adapt the code to delete anything that's NOT a certain word, or that contains a certain word?
 
Upvote 0
For NOT certain word, change the "=" to "<>", i.e.
VBA Code:
Cells(9, c).Value = "REMOVE"
becomes
VBA Code:
Cells(9, c).Value <> "REMOVE"

For contains a certain word, use the INSTR function, i.e.
VBA Code:
If InStr(Cells(9, c).Value, "Remove") > 0 Then
 
Upvote 0

Forum statistics

Threads
1,214,970
Messages
6,122,514
Members
449,088
Latest member
RandomExceller01

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