+ Reply to Thread
Results 1 to 7 of 7

Delete multiple columns based on value in first row

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-18-2013
    Location
    Prague
    MS-Off Ver
    Excel 2013
    Posts
    161

    Delete multiple columns based on value in first row

    Hi all,

    I have big table with many columns. From those columns I need to keep only several. The rest I need to delete. Can anyone help me with a macro which will delete all necessary columns.

    The table I have is in sheet “Table”, columns A to ZZ. The number of rows can vary. Column names are in the first row (A1:ZZ1).
    The list with all column names that I need to keep is in the sheet “Required columns”, range A1:A12.

    Thank you in advance for your help.

    Igor

  2. #2
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Delete multiple columns based on value in first row

    Can you provide the list in Sheet "Required Columns" ?

  3. #3
    Forum Expert sourabhg98's Avatar
    Join Date
    10-22-2014
    Location
    New Delhi, India
    MS-Off Ver
    Excel 2007, 2013
    Posts
    1,899

    Re: Delete multiple columns based on value in first row

    Assuming you have data in Sheet named >> Data
    and list in Sheet named >> List
    Use this macro-
    Sub deletecolumns()
    For n = 1 To 12
    found = Application.Match(Sheets("List").Range("A" & n).Value, Sheets("Data").Range("1:1"), 0)
    If IsError(found) Then
    Else
    Sheets("Data").Columns(found).Delete
    End If
    Next n
    End Sub
    Attached Files Attached Files
    Happy to Help

    How to upload excel workbooks at this forum - http://www.excelforum.com/the-water-...his-forum.html

    "I don't get things easily, so please be precise and elaborate"

    If someone's post has helped you, thank by clicking on "Add Reputation" below the post.
    If your query is resolved please mark the thread as "Solved" from the "Thread Tools" above.

    Sourabh

  4. #4
    Forum Contributor
    Join Date
    01-18-2013
    Location
    Prague
    MS-Off Ver
    Excel 2013
    Posts
    161

    Re: Delete multiple columns based on value in first row

    Hi Sourabhg98,
    The code works good, but I need to keep the columns that are in the list.
    If there is column which is not in the list, then to delete it.
    Thank you!
    Igor

  5. #5
    Forum Contributor
    Join Date
    01-18-2013
    Location
    Prague
    MS-Off Ver
    Excel 2013
    Posts
    161

    Re: Delete multiple columns based on value in first row

    Hi JOHN H. DAVIS,
    The list is not fixed. It can change over time. Please use the example that Sourabhg98 shared.
    Igor

  6. #6
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Delete multiple columns based on value in first row

    Maybe:

    Sub igormigor()
    Dim ws As Worksheet, ws1 As Worksheet, i As Long, y As Long, z As Long, r As Range
    Set ws = Sheets("Table")
    Set ws1 = Sheets("Required Columns")
    With ws
                For i = 1 To ws1.Range("A" & Rows.Count).End(3).row
                    Set r = .Rows(1).Find(ws1.Cells(i, 1), LookIn:=xlValues, lookat:=xlWhole)
                    If Not r Is Nothing Then r.Interior.ColorIndex = 6
                    Set r = Nothing
                Next i
            y = .UsedRange.Columns.Count
                For z = y To 1
                    Select Case .Cells(1, z).Interior.ColorIndex
                        Case Is = 6
                            GoTo zz
                        Case Else
                            .Columns(z).Delete
                    End Select
    zz:
                Next z
                .Rows(1).Interior.ColorIndex = xlNone
    End With
    
    End Sub
    Depending on your Sheet's formatting.

  7. #7
    Forum Contributor
    Join Date
    01-18-2013
    Location
    Prague
    MS-Off Ver
    Excel 2013
    Posts
    161

    Re: Delete multiple columns based on value in first row

    your code did not worked fully. I've made some changes. Its little messy, but it works.

    Thank you JOHN H. DAVIS and Sourabhg98 for your help.

    Sub igormigor()
    Dim ws As Worksheet, ws1 As Worksheet, i As Long, y As Long, z As Long, r As Range
    Set ws = Sheets("Table")
    Set ws1 = Sheets("Required Columns")
    
      ws.Range("1:1").Interior.ColorIndex = 10
    With ws
                For i = 1 To ws1.Range("A" & Rows.Count).End(3).Row
                    Set r = .Rows(1).Find(ws1.Cells(i, 1), LookIn:=xlValues, lookat:=xlWhole)
                    If Not r Is Nothing Then r.Interior.ColorIndex = 6
                    Set r = Nothing
                Next i
            
            y = .UsedRange.Columns.Count
                For z = y To 1
                    Select Case .Cells(1, z).Interior.ColorIndex
                        Case Is = 6
                            GoTo zz
                        Case Else
                            .Columns(z).Delete
                    End Select
    zz:
                Next z
    End With
    
    With ws
                For i = 1 To 100
                    If Sheets("Table").Range("a" & i).Interior.ColorIndex = 10 Then
                                            .Columns(z).Delete
                    End If
                Next i
    End With
     
      x = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Column
     
    xxx:
     For i = 1 To x
            If ws.Cells(1, i).Interior.ColorIndex <> "6" Then
           Columns(i).EntireColumn.Delete
           i = i + 1
        End If
    
    Next i
    
        test1 = WorksheetFunction.CountA(ws.Range("1:1"))
        test2 = WorksheetFunction.CountA(ws1.Range("A:A"))
    
            If test1 = test2 Then
                Else
                GoTo xxx
            End If
      ws.Range("1:1").Interior.ColorIndex = xlNone
    
    End Sub

+ 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. [SOLVED] Delete rows based on the value of two columns using VBA code - Multiple worksheets
    By OmniBlue in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-18-2015, 04:24 PM
  2. [SOLVED] Delete row based on text criteria in multiple columns
    By Jim885 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-23-2015, 12:16 AM
  3. Find and Delete rows based on criteria in multiple columns
    By Doctor_H in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-01-2013, 08:03 PM
  4. [SOLVED] how to delete data in two columns based on multiple criteria?
    By marreco in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-21-2013, 09:01 PM
  5. [SOLVED] Delete multiple empty cells in multiple columns and moving data up, witout Macros
    By CoraF in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 10-30-2012, 08:23 AM
  6. 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
  7. Delete multiple columns by column name no criteria need, just delete them
    By duugg in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-24-2009, 10:40 AM

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