+ Reply to Thread
Results 1 to 4 of 4

Delete Entire Column Based on Cell Value

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-18-2012
    Location
    Australia
    MS-Off Ver
    Excel 2016
    Posts
    192

    Delete Entire Column Based on Cell Value

    Hi Friends

    I am using the below code to delete columns which contain specific value. However I have to run the same code multiple times to get the final result. Can you please advise what's wrong with this? Also, If I want to put 2 conditions one being "Gross" and other being "Semi-Gross", what will be the code?

    Sub DeleteColumn()
    
    Set MR = Range("A2:AV2")
    
    For Each cell In MR
        If cell.Value = "Gross" Then cell.EntireColumn.Delete
    Next
     
    End Sub
    Thanks a lot for your time and effort.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,914

    Re: Delete Entire Column Based on Cell Value

    The For loop is going to get confused if you delete columns in the middle of the range it's iterating. Let's say you are checking column B, and you need to delete it. The For loop is going to go to column C next, except the original column C is now column B because you deleted column B. But the loop is now going to go to what is now column C, so the "new" column B is going to be skipped.

    The standard practice for deleting things is to start from the end and work backwards, to avoid that kind of thing. I have NOT tested this code but would be happy to do so if you provide your file.
    Dim Cell As Range ' Use Option Explicit and always declare your variables!
    Dim i As Long
    Dim MR As Range
    
    Set MR = Range("A2:AV2")
    
    For i = MR.Count To 1 Step -1
    
       Set Cell = MR(i)
       If Cell.Value = "Gross" Or Cell.Value = "Semi-Gross" Then Cell.EntireColumn.Delete
    
    Next i
    Last edited by 6StringJazzer; 04-10-2019 at 09:15 PM. Reason: made it more like the original
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Contributor
    Join Date
    07-18-2012
    Location
    Australia
    MS-Off Ver
    Excel 2016
    Posts
    192

    Re: Delete Entire Column Based on Cell Value

    Thank you very much Jeff..I really appreciate your time and effort. The code you provided is the one I wanted. It works perfect. Thanks so much buddy.

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,914

    Re: Delete Entire Column Based on Cell Value

    Glad it worked! You're welcome, and thanks for the rep.

+ 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. if cell value is XY delete entire column
    By ReneK in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-14-2018, 09:02 AM
  2. [SOLVED] Delete entire row based on value in Column
    By Mnet in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-03-2017, 06:27 PM
  3. Need macro to delete entire row based on column name.
    By Revathi kannan in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-24-2016, 01:35 PM
  4. [SOLVED] Macro to Delete Entire Row Based on Formula in Given Column
    By datutt in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-19-2015, 10:12 PM
  5. How to efficiently delete entire rows based on duplicates in one column
    By HughManatee in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-16-2013, 06:53 PM
  6. [SOLVED] Marco to delete entire row based on value in specific column
    By djm601 in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 01-17-2013, 12:53 PM
  7. Replies: 3
    Last Post: 04-19-2012, 10:45 AM

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