Delete Non-contiguous Columns in a Table, where Header is not "X,Y,Z" as a VBA script/Macro

Heretical1

New Member
Joined
Jun 3, 2020
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Good Morning amazing people,

I'm hoping I have the title right and am hoping for some assistance as I am a relatively new excel user with a bit of a challenge.

I need a relatively simplified (eg easy to understand) VBA script that will delete all columns in a table that do not contain the headers X,Y,Z etc (I have about 9 column headers that I need to keep in a table)
I have read up on deleting from the right, Select Case, Union and Ranges and my head is still spinning slightly trying to put it all together.

Is there anyone that might be able to provide some assistance with the above, I would be eternally grateful.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
I think the step with description clear enough

VBA Code:
Sub DelColumn()

Dim Col As Long, LastCol As Long
Dim ColToDelete As Range
Dim ws As Worksheet

Application.ScreenUpdating = False

'Define working sheet
Set ws = ActiveWorkbook.Sheets("Sheet1")

'Find Last Column with Headers. Assuming all Headers are in row 1
LastCol = ws.Range("A1").End(xlToRight).Column

'Loop and group all columns to be deleted
For Col = LastCol To 1 Step -1                           'Loop from right to left
    Select Case Cells(1, Col)
        Case "X", "Y", "Z"                                         ' Don't do anything if these Headers are found
        
        Case Else
            If ColToDelete Is Nothing Then              'Initial execution when ColToDelete is still empty
                Set ColToDelete = ws.Columns(Col)
            Else
                Set ColToDelete = Application.Union(ColToDelete, ws.Columns(Col))
            End If
    End Select
Next

'Delete grouped columns if there is any
If Not ColToDelete Is Nothing Then
    ColToDelete.Delete
End If

End Sub
 
Upvote 0
I think the step with description clear enough

VBA Code:
Sub DelColumn()

Dim Col As Long, LastCol As Long
Dim ColToDelete As Range
Dim ws As Worksheet

Application.ScreenUpdating = False

'Define working sheet
Set ws = ActiveWorkbook.Sheets("Sheet1")

'Find Last Column with Headers. Assuming all Headers are in row 1
LastCol = ws.Range("A1").End(xlToRight).Column

'Loop and group all columns to be deleted
For Col = LastCol To 1 Step -1                           'Loop from right to left
    Select Case Cells(1, Col)
        Case "X", "Y", "Z"                                         ' Don't do anything if these Headers are found
       
        Case Else
            If ColToDelete Is Nothing Then              'Initial execution when ColToDelete is still empty
                Set ColToDelete = ws.Columns(Col)
            Else
                Set ColToDelete = Application.Union(ColToDelete, ws.Columns(Col))
            End If
    End Select
Next

'Delete grouped columns if there is any
If Not ColToDelete Is Nothing Then
    ColToDelete.Delete
End If

End Sub
This is great, thankyou so much. I notice that I get error 1004 " Delete method of Range class failed" if I run this against a Table.
It seems to work fine if I convert the Table back to Range.

What would I be missing from the above to cause the error?
 
Upvote 0
Forget about Union. Try this. See if this is what you need

VBA Code:
Sub DelColumn()

Dim Col As Long, LastCol As Long
Dim ColToDelete As Range
Dim tbl As ListObject
Dim ws As Worksheet

Application.ScreenUpdating = False

'Define working sheet
Set ws = ActiveWorkbook.Sheets("Sheet1")

Set tbl = ws.ListObjects("Table1")

'Find Last Column with Headers. Assuming all Headers are in row 1
LastCol = ws.Range("A1").End(xlToRight).Column

'Loop and group all columns to be deleted
For Col = LastCol To 1 Step -1                           'Loop from right to left
    Select Case Cells(1, Col)
        Case "X", "Y", "Z"                                       ' Don't do anything if these Headers are found
        
        Case Else
            tbl.ListColumns(Col).Delete
    End Select
Next

End Sub
 
Upvote 0
Solution
Forget about Union. Try this. See if this is what you need

VBA Code:
Sub DelColumn()

Dim Col As Long, LastCol As Long
Dim ColToDelete As Range
Dim tbl As ListObject
Dim ws As Worksheet

Application.ScreenUpdating = False

'Define working sheet
Set ws = ActiveWorkbook.Sheets("Sheet1")

Set tbl = ws.ListObjects("Table1")

'Find Last Column with Headers. Assuming all Headers are in row 1
LastCol = ws.Range("A1").End(xlToRight).Column

'Loop and group all columns to be deleted
For Col = LastCol To 1 Step -1                           'Loop from right to left
    Select Case Cells(1, Col)
        Case "X", "Y", "Z"                                       ' Don't do anything if these Headers are found
       
        Case Else
            tbl.ListColumns(Col).Delete
    End Select
Next

End Sub
That works a treat, thankyou so much !!!!!
 
Upvote 0

Forum statistics

Threads
1,214,870
Messages
6,122,021
Members
449,060
Latest member
LinusJE

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