Delete Only Colums containing These words

berty2000

Board Regular
Joined
Mar 29, 2011
Messages
71
Hi guys

Im trying to delete a couple of columns that contain certain words but cant seem to get it to work.
This is what i got so far

Code:
Set MR = Range("R10:DA80")    
For Each cell In MR
If cell.Value = "Total Prod Changes" or "All Prod" Then cell.EntireColumn.Delete
Next
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try:
Code:
Sub deleteCols()
    Application.ScreenUpdating = False
    Dim x As Long, TPC As Range, AP As Range
    For x = 105 To 18 Step -1
        If Not Columns(x).Find("Total Prod Changes") Is Nothing Or Not Columns(x).Find("All Prod") Is Nothing Then
            Columns(x).Delete
        End If
    Next x
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
You are very welcome. :)
You replace this line of code:
Code:
 Dim x As Long, TPC As Range, AP As Range
with this line:
Code:
Dim x As Long
 
Last edited:
Upvote 0
For those who might find this interesting, the OP's request can be accomplished using a non-looping macro as well...
Code:
Sub DeleteCertainColumns()
  With Range("R10:DA80")
    .Replace "All Prod", "#N/A", xlWhole, , False, , False, False
    .Replace "Total Prod Changes", "#N/A", xlWhole, , False, , False, False
    Intersect(.Rows(1), .SpecialCells(xlConstants, xlErrors).EntireColumn).EntireColumn.Delete
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,179
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