If header title is not in the array then delete column?

dataoli

New Member
Joined
Aug 21, 2018
Messages
6
As the title would suggest i want to remove any columns that do not match an array. I tried the following code but receive an error, does anybody have a possible solution?

Code:
    Dim arr As Variant
    arr = Array("Title", "Forename", "Surname", "Address 1", "Address 2", "Address 3", "Address 4", "Town", "County", "Postcode")
    Set arrRange = RptSht.Range(RptSht.Cells(1, 1), RptSht.Cells(1, 10))
    For Each cell In arrRange
        If IsError(Application.Match(cell.Value, arr, 0)) Then
            cell.EntireColumn.Delete
        End If
    Next

I also tried using column numbers but I couldn't get it to work either!

Code:
Dim ColNum As Long, arr As Variant
    arr = Array(1, 2, 3, 4, 7, 8, 9, 10, 11, 12,13)
    For ColNum = LstCol To 1 Step -1
        If RptSht.Columns(ColNum) <> arr Then
            RptSht.Columns(ColNum).Delete
        End If
    Next
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Thanks for the replies guys

Apologies I thought the question varied enough to warrant its own thread, I also wasn't sure on the rules about posting multiple questions in the same thread.

The codes run through 300+ headings but the first only removes about 6 columns then stops, the second code skips over completely.
 
Upvote 0
You could try like this. Obviously assign RptSht first:

Code:
Dim arr As Variant, rng As Range
arr = Array("Title", "Forename", "Surname", "Address 1", "Address 2", "Address 3", "Address 4", "Town", "County", "Postcode")
Set arrRange = RptSht.Range(RptSht.Cells(1, 1), RptSht.Cells(1, 10))

For Each cell In arrRange
    If IsError(Application.Match(cell.Value, arr, 0)) Then
        If Not rng Is Nothing Then
            Set rng = Union(rng, cell)
        Else
            Set rng = cell
        End If
    End If
Next
If Not rng Is Nothing Then rng.EntireColumn.Delete Shift:=xlToLeft
 
Upvote 0

Forum statistics

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