Delete multiple columns with Criteria

Miya

Well-known Member
Joined
Nov 29, 2008
Messages
662
Hi, i have multiple columns with headers titled Total, i tried using the below code, but only one column with title Total gets deleted, how can i get the code to delete muliple columns?

Hdrs = Array("Total")
For i = LBound(Hdrs) To UBound(Hdrs)
Set Found = .Rows(6).Find(what:=Hdrs(i))
If Not Found Is Nothing Then Found.EntireColumn.Delete
Next i
 
Last edited:

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.

jbeaucaire

Well-known Member
Joined
May 8, 2002
Messages
6,012
Try this instead:
Code:
Sub DeleteTotalColumns()
Dim LastColumn As Integer
Dim rngcols As Range, cell As Range

LastColumn = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, _
            SearchDirection:=xlPrevious).Column

Set rngcols = Range(Cells(1, 1), Cells(1, LastColumn))

    For Each cell In rngcols
        If cell.Value = "Total" Then
        cell.EntireColumn.Delete
        End If
    Next cell
End Sub
 
Last edited:
Upvote 0

Miya

Well-known Member
Joined
Nov 29, 2008
Messages
662
Thanks for this, question -is this code flexible, say if i want to add more criteria to the below, then is it simply this?


f cell.Value = "Total","Average" Then


Try this instead:
Code:
Sub DeleteTotalColumns()
Dim LastColumn As Integer
Dim rngcols As Range, cell As Range
 
LastColumn = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, _
            SearchDirection:=xlPrevious).Column
 
Set rngcols = Range(Cells(1, 1), Cells(1, LastColumn))
 
    For Each cell In rngcols
        If cell.Value = "Total" Then
        cell.EntireColumn.Delete
        End If
    Next cell
End Sub
 
Upvote 0

jbeaucaire

Well-known Member
Joined
May 8, 2002
Messages
6,012
You can change the code each time you use it, or add OR commands into it
Code:
If cell.Value = "Total" Or cell.Value = "Average" Then
 
Upvote 0

Forum statistics

Threads
1,190,836
Messages
5,983,174
Members
439,825
Latest member
AdamS92

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
Top