1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Deleting entire rows given a condition

Discussion in 'Business Applications' started by PincivMa, Apr 3, 2008.

Thread Status:
Not open for further replies.
Advertisement
  1. PincivMa

    PincivMa Thread Starter

    Joined:
    Mar 13, 2004
    Messages:
    378
    Hi There

    I have created a macro that deletes entire rows when it meets a certain condition. The condition is as follows:

    The macro starts in Cell G2. In column G, if the Active Cell is > 730 then delete the entire row. Else If the Active Cell <= 730 then go down to next cell. Some cells in column G are blank. They have no data. I have over 55, 000 rows of data and the macro that I did takes forever to run since it checks every single cell. I stops when the last cell in column A is a blank.

    Is there a macro that can do this quickly? In case you are interested below is the macro that I wrote.

    Range("G2").Select
    Do
    ActiveCell.Select
    If ActiveCell > 730 Then
    ActiveCell.EntireRow.Delete
    ElseIf ActiveCell <= 730 Then
    ActiveCell.Offset(1, 0).Select
    End If
    Loop Until ActiveCell.Offset(0, -6) = ""


    Mario
     
  2. Ziggy1

    Ziggy1

    Joined:
    Jun 17, 2002
    Messages:
    2,551
    Can you sort your Data prior to running the Delete? This will move all the empty cells to the bottom...then it will stop when it hits the empty cell
     
  3. bomb #21

    bomb #21

    Joined:
    Jul 1, 2005
    Messages:
    8,546
    Try another approach.

    1. Build formulas in all the rows in the first free column. If first free column is H, then:

    =IF(G2>730,1,"X")

    would give you 1 for 740, X for blank and X for 730.

    You could do this with one code line:

    Range("A2", Range("A" & Rows.Count).End(xlUp)).Offset(, 7).FormulaR1C1 = "=IF(RC[-1]>730,1,""X"")"

    (note: this will build the formulas in column H)

    For my 55000 test rows, it takes < 2 seconds to execute.

    The next step would be:

    Range("H2", Range("H" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeFormulas, 1).Select

    , followed by deleting the selected rows ... and then column H.

    HTH (make sure your work is "safe" before you try anything :) )
     
  4. bomb #21

    bomb #21

    Joined:
    Jul 1, 2005
    Messages:
    8,546
    Also, re: "takes forever" -- possibly you have loads of formulas & it's recalculating the entire sheet every time you delete a row. Consider toggling automatic calculation off and on.
     
  5. PincivMa

    PincivMa Thread Starter

    Joined:
    Mar 13, 2004
    Messages:
    378
    Hi Bomb #21

    It worked!! Thanks.
     
  6. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Similar Threads - Deleting entire rows
  1. ImaGrump
    Replies:
    2
    Views:
    644
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/700003

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice