Macro to delete specific col Headers

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,595
Office Version
  1. 2021
Platform
  1. Windows
I have headers in row 1 on sheet "Data Import"


I have written code to delete a specific Col Header, but need it to delete several Col Headers for eg Physical address, Remarks, branch ID etc


Code:
 Sub DeleteSpecifcColumn()


Set MR = Range("A1:z1")
    For Each cell In MR
        If cell.Value = "Date" Then cell.EntireColumn.Delete
    Next
End Sub


It would be appreciated if someone could amend my code
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Maybe...

Code:
Sub DeleteSpecifcColumns()
    Dim MR As Range, ColToDel As Range, v As Variant

    Set MR = Sheets("Data Import").Range("A1:Z1")
    
    For Each v In Array("Date", "Physical address", "Remarks", "branch ID")
        Set ColToDel = MR.Find(v, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns, _
                SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
        If Not ColToDel Is Nothing Then ColToDel.EntireColumn.Delete
    Next v
End Sub

M.
 
Upvote 0
For those who might find this interesting, the macro to do what the OP asked for can be reduced to two lines of code (no loops involved); however there are limitations to the method I used (see the notes below)...
Code:
[table="width: 500"]
[tr]
	[td]Sub DeleteSpecifcColumns()
  Intersect(Rows(1), Range(Replace(Mid(Join([IFERROR(REPT(":"&CHAR(64+MATCH({"Date","Physical Address","Remarks","branch ID"},A1:Z1,0)),2),"")], ","), 2), ",:", ","))).Value = "#N/A"
  Rows(1).SpecialCells(xlConstants, xlErrors).EntireColumn.Delete
End Sub[/td]
[/tr]
[/table]

Note 1
------------------------
Any columns to be deleted must lie within the column range A:Z

Note 2
------------------------
If additional headers are to be added to the list, they will go into the list highlighted in blue above with quotes around them and commas separating them. However, there is a size limitation that must be observed. The total number of characters for any additional headers plus the quote marks surrounding them plus the commas separating them cannot total more than 158 characters.
 
Upvote 0
Thanks for your code Rick

Always very interesting
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,262
Members
452,627
Latest member
KitkatToby

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