Consulting

Results 1 to 4 of 4

Thread: Solved: Delete Column that do not have headers "XYZ"

  1. #1
    VBAX Regular
    Joined
    Oct 2004
    Posts
    65
    Location

    Solved: Delete Column that do not have headers "XYZ"

    I have a sheet that over 60 Column's each one has a header then data.
    I only need to keep the data that has the following headers in row 1:

    WeekNum
    ROUTING_SET
    Date
    lookup
    Day
    Interval
    HOUR MINUTE
    A SCH ADJ
    RF AHT
    RF NCO
    Everything else can be deleted, does any one have any VBA code that can help with this task?

    thanks
    We are living in a world today
    where lemonade is made from
    artificial flavoring and furniture polish
    is made from real lemons...
    Alfred E Newman

  2. #2
    VBAX Mentor Teeroy's Avatar
    Joined
    Apr 2012
    Location
    Sydney, Australia
    Posts
    414
    Location
    Try on a copy first. It will affect only the Activesheet.

    [vba]Sub deleteColumnsNotRequired()
    Dim Reqd, rng As Range
    Reqd = Array("WeekNum", "ROUTING_SET", "Date", "Lookup", "Day", "Interval", "Hour Minute", _
    "A SCH ADJ", "RF AHT", "RF NCO")
    For Each rng In Range("A1:" & Cells(1, Columns.Count).End(xlToLeft).Address)
    If IsError(Application.Match(rng.Value, Reqd, 0)) Then
    rng.EntireColumn.Delete
    End If
    Next
    End Sub[/vba]
    _________________________________________________________________________
    "In theory there is no difference between theory and practice. In practice there is." - Chuck Reid

    Any day you learn something new is a day not wasted.

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    [vba]
    sub M_snb()
    c00="|WeekNum|ROUTING_SET|Date|lookup|Day|Interval|HOUR MINUTE|A SCH ADJ|RF AHT|RF NCO|"

    for each cl in rows(1).specialcells(2)
    if instr(c00,"|" & cl.value & "|")=0 then cl.value=""
    next

    rows(1).specialcells(4).entirecolumn.delete
    end sub
    [/vba]

  4. #4
    VBAX Regular
    Joined
    Oct 2004
    Posts
    65
    Location
    Both worked thanks a lot
    We are living in a world today
    where lemonade is made from
    artificial flavoring and furniture polish
    is made from real lemons...
    Alfred E Newman

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •