ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Delete multiple columns based on certain criteria (https://www.excelbanter.com/excel-programming/385891-delete-multiple-columns-based-certain-criteria.html)

Gretta

Delete multiple columns based on certain criteria
 
I need to delete several non-adjacent columns. I would like to delete based
upon the column name, which will always be in the first row, but the columns
are not necessarily in the same order.

How can I best automate this?

TIA

Gretta

Bernie Deitrick

Delete multiple columns based on certain criteria
 
Gretta,

Change the value of the myFindString from this:

myFindString = "Delete me"

to the name that needs deleting, and run the macro below.

HTH,
Bernie
MS Excel MVP

Sub DeleteColumns()
Dim c As Range ' The cell found with what you want
Dim d As Range ' All the cells found with what you want
Dim myFindString As String
Dim firstAddress As String

myFindString = "Delete me"

'Find all the cells with the delete phrase
With Range("1:1")

Set c = .Find(myFindString, LookIn:=xlValues, lookAt:=xlWhole)

If Not c Is Nothing Then
Set d = c
firstAddress = c.Address
Else:
MsgBox "Not Found"
End
End If

Set c = .FindNext(c)
If Not c Is Nothing And c.Address < firstAddress Then
Do
Set d = Union(d, c)
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address < firstAddress
End If
End With

'Now delete those columns
d.EntireColumn.Delete

End Sub



"Gretta" wrote in message
...
I need to delete several non-adjacent columns. I would like to delete based
upon the column name, which will always be in the first row, but the columns
are not necessarily in the same order.

How can I best automate this?

TIA

Gretta





All times are GMT +1. The time now is 05:32 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com