Delete Whole Column If Header Contains "x"

  • Re: Delete Whole Column If Header Contains "x"


    pepperell


    try:


    Code
    1. Sub remove_columns()
    2. For i = ActiveSheet.Columns.Count To 1 Step -1
    3. If InStr(1, Cells(1, i), "%") Then Columns(i).EntireColumn.Delete
    4. Next i
    5. End Sub


    filippo

  • Re: Delete Whole Column If Header Contains "x"


    Hi,


    THis will cycle through the first 10 columns if it finds % in the top cell it will delete the column.


    Does that help?


    Code
    1. Sub DeleteColumns()
    2. DIm i as integer, A as range
    3. For i = 10 To 1 Step -1
    4. Set a = Cells(1, i).Find(What:="%", LookIn:=xlValues)
    5. If Not a Is Nothing Then a.EntireColumn.Delete
    6. Next i
    7. End Sub


    Edit: I think filo's method above is better.

  • Re: Delete Whole Column If Header Contains "x"


    A compromise that does all columns without checking every column.
    [vba] Dim A As Range

    Do
    Set A = Rows(1).Find(What:="%", LookIn:=xlValues, lookat:=xlPart)
    If A Is Nothing Then Exit Do
    A.EntireColumn.Delete
    Loop
    [/vba]

  • Re: Delete Whole Column If Header Contains "x"



    Now were talking. : D