+ Reply to Thread
Results 1 to 8 of 8

delete entire row if 'value' found anywhere in column - most efficient way?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-29-2011
    Location
    Scotland
    MS-Off Ver
    Excel 2003
    Posts
    122

    delete entire row if 'value' found anywhere in column - most efficient way?

    Ive got the code below doing what i want, which is to delete an entire row if 'Unsettled' is found anywhere in a particular column.

    Is there a more efficient way to do this when the number of rows is large?

    Dim Counter As Long
    
    Cells(2,colNum).Select    ' Starting point, row 2 in column 'colNum'
    
     For Counter = 1 To 25000     ' need to change 25000 to lastRow later
      If ActiveCell.Value Like "Unsettled" Then
       ActiveCell.EntireRow.Delete     ' Delete Row if 'Unsettled' is found in colNum
        Else
         ActiveCell.Offset(1, 0).Select     ' Move down a row
          End If
      Next Counter
    Last edited by intothewild; 11-24-2012 at 02:18 PM.

  2. #2
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: delete entire row if 'value' found anywhere in column - most efficient way?

    Give this a try

    Sub del_unwantedRows()
    Const WhatColumn As String = "A" '<== Change to the column that contains Unsettled
    Dim RangeToDelete As Range
    Dim lastrow As Long, Ptr As Long
        
        lastrow = Cells(Rows.Count, WhatColumn).End(xlUp).Row
        For Ptr = 1 To lastrow
            If Cells(Ptr, WhatColumn) Like "*Unsettled*" Then
                If RangeToDelete Is Nothing Then
                    Set RangeToDelete = Cells(Ptr, WhatColumn)
                Else
                    Set RangeToDelete = Union(RangeToDelete, Cells(Ptr, WhatColumn))
                End If
            End If
        Next Ptr
        If Not RangeToDelete Is Nothing Then
            RangeToDelete.EntireRow.Delete
        End If
    End Sub
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  3. #3
    Forum Contributor
    Join Date
    06-29-2011
    Location
    Scotland
    MS-Off Ver
    Excel 2003
    Posts
    122

    Re: delete entire row if 'value' found anywhere in column - most efficient way?

    Thats where i was headed, thanks Mike.

    However, what i was asking was not this, but... is there a more efficient way to perform this task than going down the column cell by cell.

    For example, in vba, would filtering and deleting the visible cells be quicker?
    Or is there a completely different approach that is more efficient on resources/time to run.
    The reason i ask is that i may have upto 30000 rows to process and may be performing the routine multiple times daily.
    Last edited by intothewild; 11-24-2012 at 02:47 PM.

  4. #4
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: delete entire row if 'value' found anywhere in column - most efficient way?

    The solution I gave would be way faster then what you have. Yes you can delete filtered rows, but I believe you would need to sort that column first then filter the data and delete. Specialcells(xlCellTypeVisible) is funny when dealing with very large amount of rows.

  5. #5
    Forum Contributor
    Join Date
    06-29-2011
    Location
    Scotland
    MS-Off Ver
    Excel 2003
    Posts
    122

    Re: delete entire row if 'value' found anywhere in column - most efficient way?

    true, forgot 'special cells visible' can be funny at times, thanks.
    the question remains as to whether your improved method (cheers for that!) is 'optimal'?
    this part is a small part of a much larger process which is taking too long so need to optimise all parts so the sum is faster.
    Last edited by intothewild; 11-24-2012 at 03:03 PM.

  6. #6
    Forum Contributor
    Join Date
    06-29-2011
    Location
    Scotland
    MS-Off Ver
    Excel 2003
    Posts
    122

    Re: delete entire row if 'value' found anywhere in column - most efficient way?

    ok, for test purposes (time to run)....im going to use this...

    ' Start the Timer
    start_time = Now()
    
    '''''''''''''''' 
    'Production code here
    '''''''''''''''
    
    'Stop the Timer
    end_time = Now()
     
    Application.StatusBar = "Completed calculations. Time taken: " & (DateDiff("s", start_time, end_time) & " seconds")
    Would be good to get more methods to test though.
    Last edited by intothewild; 11-25-2012 at 07:18 AM.

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

    Re: delete entire row if 'value' found anywhere in column - most efficient way?

    Mike,
    Thanks for bringing up the issue of filter!
    I know many people in this forum prefer filter to loop and they have their reasons and justifications.
    One of the main advantages claimed by filter users is its speed.
    Back in 2007, when MS came up with a table, I heavily used tables at work to store large data. I had problems with sorting data. As you said, it sometimes behaved in a funny way. At the time, I was not sure what the problem was. It was only when I joined this forum, someone sent me link to the problem with filtering and sorting in table excel 2007.
    I have not learned the syntax for filtering; partly my mind is still haunted by the experience of table 2007. I am sure filtering is great, but I personally feel my data and formulas would be over the place after the filter, so will stick with a loop until I overcome my phobia with it.

  8. #8
    Registered User
    Join Date
    11-29-2008
    Location
    FRANCE
    Posts
    4

    Re: delete entire row if 'value' found anywhere in column - most efficient way?

    Hi

    Another way for deleting the rows
    Sub test()
    DelRows 1
    End Sub
    Private Sub DelRows(Ncol&)
    Dim rng As Range
    Set rng = Cells(1, Ncol).Resize(Cells(Rows.Count, Ncol).End(xlUp).Row)
    On Error Resume Next
    With rng.Offset(, 1)
        .FormulaR1C1 = "=IF(ISERROR(SEARCH(""*Unsettled*"",RC[-1])),"""",1)"
        .Value = .Value
        .SpecialCells(xlCellTypeConstants, 1).EntireRow.Delete
    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