I am trying to find a way to delete the entire column in excel macro based on column header value that contains some string.
Note: If the column header appears n number of times, it has to find and delete n number of columns in vb macro.
Let's say i have a Column header called "COLUMN_6" which appears two times, then my code has to find the column header that contains COLUMN_6 and it has to delete these two columns.
I have written the below code and it's working partially.
Sub ClearSpecificColumns()
Dim last_col As Long
'get the last column
last_col = Worksheets("Sheet1").Cells(1, Columns.Count).End(xlToLeft).Column
MsgBox ("222"& last_col)
'iterate from 1 to last column
For i = 1 To last_col
If Cells(1, i).Value Like "COLUMN_6" Then
Columns(i).Delete
End If
Next
End Sub
Problems:
The above code finds and deletes ONLY ONE TIME (the first appearance only) the entire column with COLUMN_6. It's supposed to delete both the two columns.
why it's NOT iterating for the 2nd time to delete the second appearance of COLUMN_6 in this case.
Any other easy and fast way to do this?