Find text in string and delete columns without the text

winspear

New Member
Joined
Aug 6, 2009
Messages
12
I have a sheet where (starting at column E), each alternate column has a header.

eg

E F G H etc.....
31_02_Datannn (blank) 31_02_Field (blank)

I need code which (starting at column E) looks for the word "Field" within the column headers and if found, does nothing but skips to the next column with a header. If "Field" is not found, I need to delete both the column with the heading and the one to the right of it - then continue the same check until the last column is reached.

I have the following work in progress code (which currently does not work) - can anyone help please?

LastColumn = Range("IV1").End(xlToLeft).Column
ColumnNumber = 5 'The First column number with an object in the heading

ValIn = Cells(1, ColumnNumber)

Do While ColumnNumber <= LastColumn

If InStr(1, ValIn, "Field", vbTextCompare) = 0 Then
Range("ColumnNumber:ColumnNumber + 1").Delete
ColumnNumber = ColumnNumber + 2
MsgBox (ColumnNumber)
End If

Loop
 
Last edited:

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Hi
try these codes
Code:
Sub winspear()
Dim a As Long, k As Long
Dim J As String
For a = Range("IV1").End(xlToLeft).Column To 5 Step -2
If InStr("Field", Cells(1, a)) = 0 Then
    If a <= 26 Then
    J = Chr(a + 64)
    Else
    k = a Mod 26
        If k = 0 Then
        k = 26
        End If
    J = Chr(Round((a / 26) + 0.49, 0) + 63) & Chr(k + 64)
    End If
Range(J & ":" & J).EntireColumn.Delete
End If
Next a
MsgBox "Complete"
End Sub
ravi
 
Upvote 0

Forum statistics

Threads
1,223,054
Messages
6,169,835
Members
452,284
Latest member
TKM623

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