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

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
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,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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