+ Reply to Thread
Results 1 to 2 of 2

Thread: Macro to delete all columns except specified headers

  1. #1
    Registered User
    Join Date
    01-26-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    8

    Macro to delete all columns except specified headers

    Hey there everyone,

    First post on the forum, have been grabbing info here for awhile. Feel free to blow me up when I don't follow the SOP.

    I am trying to create a macro that will delete all columns aside from those specified. They must be labeled by the actual column header as data imports can vary greatly. Columns I would like to keep, based on header name are: Word , Pay, Cost, Volume, Rank, & Type

    I really have searched the forum high and low also all over google. Everything is detailed on deleting rows or deleting columns by specifying header to be deleted (not what I want to achieve).

    Thank you.
    Last edited by macattackr; 01-27-2012 at 09:33 AM.

  2. #2
    Valued Forum Contributor MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2010
    Posts
    5,300

    Re: Macro to delete all columns except specified headers

    Hi macattackr and welcome to the forum.
    Try this code in the attached sample file.
    Sub DelColumnNotInList()
    Dim LastCol As Double
    Dim ColCtr As Double
    LastCol = Cells(1, Columns.Count).End(xlToLeft).Column
        For ColCtr = 1 To LastCol
            If InStr("Word Pay Cost Volume Rank Type", Cells(1, ColCtr).Text) = 0 Then
                Cells(1, ColCtr).EntireColumn.Delete
                ColCtr = ColCtr - 1
            End If
        Next ColCtr
    End Sub
    Note - LastCol looks in Row 1 for your column headers.
    Attached Files Attached Files
    One test is worth a thousand opinions.
    Click the * below to say thanks.

  3. #3
    Registered User
    Join Date
    01-26-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: Macro to delete all columns except specified headers

    Hey Marvin that worked great....thank you...i will be submitting another question shortly! starting to learn and pick it up

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0