+ Reply to Thread
Results 1 to 3 of 3

Need help - Delete columns based on column name

Hybrid View

  1. #1
    Registered User
    Join Date
    08-01-2011
    Location
    San Francisco
    MS-Off Ver
    Excel 2007
    Posts
    2

    Need help - Delete columns based on column name

    Hi,

    I need help with writing the code for the following. All help is appreciated.

    I have a spreadsheet with probably 50 columns. There are about 10 columns that I need to keep while deleting the rest, based on the column header (first row).

    So, for example, there are ten columns with the first cells as – keep1, keep2,… etc
    And 40 columns with first cells as - delete1, delete2,… etc

    So, excel needs to loop through the first row of the spreadsheet deleting the columns whose names are not keep1, keep2 etc.

    Many thanks.

    Amit

  2. #2
    Forum Expert
    Join Date
    01-12-2007
    Location
    New Jersey
    Posts
    2,127

    Re: Need help - Delete columns based on column name

    Try something like this and report back:

    Sub DeleteStuffForAmitan()
        Dim colCnt As Long
        
        colCnt = ActiveSheet.UsedRange.Columns.Count
        
        With Application
            .ScreenUpdating = False
                
        For i = colCnt To 1 Step -1
            If InStr(1, UCase(Cells(1, i)), "DELETE") > 0 Then
                Columns(i).Delete
            End If
        Next i
            .ScreenUpdating = True
        End With
    End Sub

  3. #3
    Registered User
    Join Date
    08-01-2011
    Location
    San Francisco
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: Need help - Delete columns based on column name

    Thanks a lot for your response.

    I tried he following and it wored.


    -------------------



    Sub DeleteStuffForAmitan()

    Dim colCnt As Long


    Sheets("Sheet1").Select

    colCnt = ActiveSheet.UsedRange.Columns.Count

    With Application
    .ScreenUpdating = False

    For i = colCnt To 1 Step -1
    Cells(1, i).Select

    If _
    Cells(1, i).Value = "del1" Or _
    Cells(1, i).Value = "del2" Or _
    Cells(1, i).Value = "del3" _
    Then
    Columns(i).Delete
    End If
    Next i
    .ScreenUpdating = True
    End With

    End Sub

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