+ Reply to Thread
Results 1 to 11 of 11

Macro to delete columns based on header

  1. #1
    Registered User
    Join Date
    08-06-2013
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    5

    Macro to delete columns based on header

    Can I have a macro delete columns in a spreadsheet that I want to discard based on the column heading?
    I want the macro to delete the same headers each time it is run. The content is static: Example I want to delete columns with these headings. These should always be in the same position on the spreadsheet.

    I get spreadsheets from our IT department that have about 30-75 headings, most of which I want to automatically delete for the reports I need.

    Example:
    A1 ID
    B1 RECEIPT_NO
    C1 FIRST_NAME
    D1 LAST_NAME
    E1 SPOUSE_FIRST_NAME DELETE THIS COLUMN
    F1 SPOUSE_LAST_NAME DELETE THIS COLUMN
    G1 DATE_CREDITED
    H1 CREDIT_AMOUNT
    I1 FIRST_ENTRY
    J1 PRIMARY_STAFF
    K1 SECONDARY_STAFF

  2. #2
    Registered User
    Join Date
    11-14-2010
    Location
    Macau
    MS-Off Ver
    Excel 2003
    Posts
    66

    Re: Macro to delete columns based on header

    Please try this.
    Please Login or Register  to view this content.

  3. #3
    Forum Contributor
    Join Date
    08-11-2012
    Location
    bengalur
    MS-Off Ver
    Excel 2003, 2007
    Posts
    152

    Re: Macro to delete columns based on header

    Please Login or Register  to view this content.
    This code will delete the columns without popups displayed. In any deletion i.e., row deletion or column deletion, a popup will appear with message of deletion and you need to click OK on it.
    ______________________________________________________________________________

    if you feel this has helped you, please add to reputation

  4. #4
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Macro to delete columns based on header

    Shyamhappy,

    Not

    Please Login or Register  to view this content.
    But should be

    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    08-11-2012
    Location
    bengalur
    MS-Off Ver
    Excel 2003, 2007
    Posts
    152

    Re: Macro to delete columns based on header

    Sorry about that it was my typo error.

  6. #6
    Registered User
    Join Date
    08-06-2013
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Macro to delete columns based on header

    Hello shyamhappy,

    I renamed the macro to newdeleteheader2 and changed the range to Columns G:H. By the way, I am using Excel 2010 not 2007.

    I got a run-time error "1004"
    Select method of Range class failed.



    Sub newdeleteheader2()

    Dim Sht1 As Worksheet
    Set Sht1 = ThisWorkbook.ActiveSheet

    Application.DisplayAlerts = False

    With Sht1
    .Columns("G:H").Select
    Selection.Delete Shift:=xlToLeft
    End With

    Set Sht1 = Nothing
    End Sub

  7. #7
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Macro to delete columns based on header

    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    08-06-2013
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Macro to delete columns based on header

    Hi lutuxel,

    This worked....

    I edited it to delete a series of columns. The macro deletes the higher columns first, so that the column address isn't confused.

    Sub jw_test1()
    Dim wb As Workbook
    Dim ws As Worksheet
    Set ws = ActiveSheet

    With ws
    .Columns("AD:AE").Delete
    .Columns("V:AA").Delete
    .Columns("R:S").Delete
    .Columns("O").Delete
    .Columns("G:H").Delete
    .Columns("E").Delete
    .Columns("D").Delete
    End With
    Set wb = Nothing
    Set ws = Nothing
    End Sub

  9. #9
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Macro to delete columns based on header

    sfadam,
    Please use code tags with your code as per forum's rule.

    Please Login or Register  to view this content.

  10. #10
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Macro to delete columns based on header

    maybe as an option
    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    07-25-2012
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    1

    Re: Macro to delete columns based on header

    Macro to Compare two sheets header and delete columns where the header doesn't match

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. macro to delete blank columns - includes header row
    By bluesea in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-31-2012, 12:18 PM
  2. Replies: 2
    Last Post: 10-13-2012, 03:30 AM
  3. Macro to delete certain columns and delete rows based on time in another column
    By beepbeep27 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-12-2012, 11:47 AM
  4. Delete Columns Based on Header
    By MSmithson in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-28-2010, 03:19 AM
  5. Macro to rearrange/add columns based on column header?
    By drdavidge in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-07-2006, 09:35 PM

Tags for this Thread

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.6.0 RC 1