Try this code instead? I'm not sure what was going on in the previously
provided code, so I just wrote this from scratch. If you have any questions,
just ask.
This goes into a regular code module and you access it using Tools | Macro |
Macros
Sub DeleteMiddleColumn()
'any column that is between the two with the
'defined labels will be deleted.
'only works with 1 column between the two
'so with 'Column 1' | newCol | 'Column 2'
'labels, the 'newCol' column would be deleted
'regardless of how it is labeled.
Const keeperCol1 = "Column 1" ' change to actual label
Const keeperCol2 = "Column 2" ' change to actual label
Dim lastColumn As Long
Dim titleRange As Range
Dim anyTitle As Range
lastColumn = Range("A1").Offset(0, Columns.Count - 1).End(xlToLeft).Column
Set titleRange = Range("A1", Cells(1, lastColumn))
For Each anyTitle In titleRange
If UCase(anyTitle.Value) = UCase(keeperCol1) And _
UCase(anyTitle.Offset(0, 2).Value) = UCase(keeperCol2) Then
anyTitle.Offset(0, 1).EntireColumn.Delete
'alldone, can exit
Exit For
End If
Next
Set titleRange = Nothing
End Sub