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

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
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
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
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,214,827
Messages
6,121,803
Members
449,048
Latest member
greyangel23

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