You are here:

Excel/Deleting rows by formula

Advertisement


Question
Hi Bob,

You have been very impressive with your replies in the past. Try this one! Is it possible to delete rows by formula, as against deleting rows manually?

Here's my actual example: I dump thousands of rows into a spreadsheet as a result of a database query. Following is a small portion:

14/U13/01/10XX
--------------
60

(1 row(s) affected)

14/U13/01/20XX
--------------
60

(1 row(s) affected)

14/U13/01/30XX
--------------
0

(1 row(s) affected)

I would like to get rid of certain rows as superfluous:
1) where the cell says "(1 Row(s) affected)" I would like to delete this and, even better, the (blank) rows above and below it
2) where cell says "0" and only "0", I would like to get rid of this and the two lines above it - e.g. in this example, where the cell says "0", the lines to remove would be the "0" row itself, the "14/U13/01/30XX" row, and the row with the dotted lines in it

Sounds like a tall order to me, but see what you think!

Many thanks in anticipation

Dave


Answer
You would need VBA code to do it. If your data is in column A this (untested) macro should work:
Sub DeleteWeird()
  For i=Range("A65536").end(xlup).row to 3 step -1
    if cells(i,1).value="(1 Row(s) affected)" then
       rows(i-1).resize(3).Delete
    elseif cells(i,1).value="0" then
       rows(i-2).resize(3).Delete
    End If
  Next
End Sub
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Bob Umlas

Expertise

I`m a Microsoft Excel MVP (Most Valuable Professional) and have been since the inception of the program in 1995. I can answer every kind of Excel question except: API, Importing/exporting to other programs (powerpoint, word,...)

Experience

Worked with MS Excel since version 0.99 (on the Mac!). Was contributing editor to Excellence Magazine, having written >300 articles. John Walkenbach said of me "I finally met someone who knows as much about Excel as I do."

Publications
Excellence, The Expert, Microsoft

Awards and Honors
MVP
Led sessions for the Convergence 2004-2006 seminar on Excel tips & tricks

©2012 About.com, a part of The New York Times Company. All rights reserved.