VBA: Delete Entire Columns based on Header value in Excel

unknownymous

Board Regular
Joined
Sep 19, 2017
Messages
249
Office Version
  1. 2016
Platform
  1. Windows
Hi Gurus,

Good day!

I found below code in deleting specific column. However, I want to apply it for all tab sheets. Suppose I have 15 sheets.

= = =
Sub DeleteSpecifcColumn()

Dim xFNum, xFFNum, xCount As Integer
Dim xStr As String
Dim xArrName As Variant
Dim MR, xRg As Range

On Error Resume Next

Set MR = Range("A1:BZ1")

xArrName = Array("old", "new", "get") 'enclose each column name with double quotes and separate them by comma
xCount = MR.Count
xStr = xArrName(xFNum)

For xFFNum = xCount To 1 Step -1
Set xRg = Cells(1, xFFNum)
For xFNum = 0 To UBound(xArrName)

xStr = xArrName(xFNum)
If xRg.Value = xStr Then xRg.EntireColumn.Delete

Next xFNum

Next

End Sub

'Source: How to delete entire columns based on header value in Excel?

= = =
For the array "new" and "get", I noticed that whenever I paste it in from my sheet to VBA there's a double quote already. Adding an extra double quote is not working like:
xArrName = Array("old", ""new"", ""get"")

Any thoughts on how to update the the VBA codes above?

Thanks in advance! :)
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Try the following on a copy of your file:
VBA Code:
Option Explicit
Option Compare Text
Sub DelCols()
    Dim ws As Worksheet, DelMe As Range, LCol As Long, i As Long, j As Long, HdrArr
    HdrArr = Array("old", "new", "get")
    
    For Each ws In Worksheets
        With ws
            LCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
            For i = 1 To LCol
                For j = LBound(HdrArr) To UBound(HdrArr)
                    If .Cells(1, i) = HdrArr(j) Then
                        If DelMe Is Nothing Then
                            Set DelMe = .Columns(i)
                        Else
                           Set DelMe = Union(DelMe, .Columns(i))
                        End If
                    End If
                Next j
            Next i
        End With
        If Not DelMe Is Nothing Then DelMe.Delete
        Set DelMe = Nothing
    Next ws
End Sub
 
Upvote 0
Try the following on a copy of your file:
VBA Code:
Option Explicit
Option Compare Text
Sub DelCols()
    Dim ws As Worksheet, DelMe As Range, LCol As Long, i As Long, j As Long, HdrArr
    HdrArr = Array("old", "new", "get")
   
    For Each ws In Worksheets
        With ws
            LCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
            For i = 1 To LCol
                For j = LBound(HdrArr) To UBound(HdrArr)
                    If .Cells(1, i) = HdrArr(j) Then
                        If DelMe Is Nothing Then
                            Set DelMe = .Columns(i)
                        Else
                           Set DelMe = Union(DelMe, .Columns(i))
                        End If
                    End If
                Next j
            Next i
        End With
        If Not DelMe Is Nothing Then DelMe.Delete
        Set DelMe = Nothing
    Next ws
End Sub

This is working however, I am having a problem as the "New Data" and "Get Data" columns were not deleted probably because of the header format. Whenever I use the find option, it looks like this "Get Data" (with double quote).
 

Attachments

  • Sample Data.PNG
    Sample Data.PNG
    8.3 KB · Views: 21
Upvote 0
Try this alternative
VBA Code:
Option Explicit
Option Compare Text
Sub DelCols()
    Dim ws As Worksheet, DelMe As Range, LCol As Long, i As Long
    
    For Each ws In Worksheets
        With ws
            LCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
            For i = 1 To LCol
                
                    If .Cells(1, i) Like "*old*" Or _
                        .Cells(1, i) Like "*new*" Or _
                        .Cells(1, i) Like "*get*" Then
                        If DelMe Is Nothing Then
                            Set DelMe = .Columns(i)
                        Else
                           Set DelMe = Union(DelMe, .Columns(i))
                        End If
                    End If
            Next i
        End With
        If Not DelMe Is Nothing Then DelMe.Delete
        Set DelMe = Nothing
    Next ws
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,958
Messages
6,175,636
Members
452,662
Latest member
Aman1997

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