+ Reply to Thread
Results 1 to 4 of 4

Deleting rows containing zero within specified range

  1. #1
    Registered User
    Join Date
    02-04-2011
    Location
    england
    MS-Off Ver
    Excel 2003
    Posts
    4

    Deleting rows containing zero within specified range

    Hi, I have tried searching forum and have found lots of similar questions and answers but nothing exactly like what I would like to do. Unfortunately, I'm not experienced enough to tweak these similar queries to suit my needs.

    In cells A15:A130 I have a list of zeroes and ones. I simply want to select all the cells which contain a zero in that range and then delete those entire rows.

    I normally just record macros but I don't know how to do this even without the code. I can find and select all blanks in the range and then delete those rows, but as these cells contain a formula they are not being recognised as blanks even if I use "". Hence why I think I need to use a macro.

    If somebody could give me the necessary code that would be great and I will try to decipher it myself. Or if someone has more time, I would really appreciate an explanation of the code.

    I am a complete novice, and usually work just by recording macros.

    Thanks in advance for any help.

  2. #2
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Deleting rows containing zero within specified range

    hi, turn on Macro recording and apply autofilter with condition to show zeros, delete all autofiltered rows with zeroes, turn off autofilter. Here you are, the code is your pocket

  3. #3
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,241

    Re: Deleting rows containing zero within specified range

    Hi NewbieVBA and welcome to the forum,
    Open your sheet and click on Record Macro.Click on OK when it gives the Record Macro Name (remember this name).
    Click on the entire row 15 (click on the "15" left of A15 and delete it). Stop recording.

    Click on Visual Basic icon to see what has been recorded. You need to click on Module 1 in the Projects area if you can't find it. Here is what I recorded when I did the above.
    Please Login or Register  to view this content.
    1. Always leave the Option Explicit as the first line of every module as it will help find easy spelling bugs before you try to run your code.
    2. Sub Macro1() is the name of this Macro and you can run it from the Macros Icon
    3. Every Sub needs and "End Sub" so it knows where to stop

    This leaves only 2 lines of code you need to study.

    You wanted to do a Test to see if it was a zero or one and do all this from row 15 to 130 in youir question. Lets see if we can figure that out.

    We want the 15 from the above code to go from 15 to 130 so lets creata a variable that will cover these numbers. Do that with a DIM statement. DIM says to create a variable and also tells what type of variable it is. I like to use varialbe names that mean something to me so I'm going to use RowCtr (Row Counter to go from 15 to 130). Some guys like to use i or j or k which work fine but are a little less obvious on what they are really doing.

    "Dim RowCtr as Double" is the line that creates this variable for VBA.

    Now how dow we make this variable work in the "Rows("15:15").Select?????
    How about Rows("RowCtr:RowCtr").Select - which doesn't work..... Now what???
    Jump to the help file or internet to see how to use Rows in VBA. It says I can select a whole row by just doing a "Rows(15)" without needing two of them or the colon. I like that better. Click down to the Immediate Window (View it if not shown) and type "Rows(17).Select" and press enter. If you get an error message then VBA doesn't like what you typed. If it follows your instructions then your syntax is correct... YEH!!!

    Somehow we have to do all those rows - 15 to 130 and I like to do a For Next Loop. The code would look like:
    Please Login or Register  to view this content.
    Now for the if what is in the A Column =0 or not part of the code.
    In the Immediate Window do a "Debug.Print Range("A17")" and press enter. VBA likes this statement and returns a 1 or zero. I can use this type of statement in the VBA to get to...
    Please Login or Register  to view this content.
    Note that I indented each section of the Loop and If statments so it is easy to understand where it ends.

    After running this code I see there are some problems. THIS ALWAYS HAPPENS!!! Don't let programmers tell you their code works perfectly the first time!!!

    The problem after using BreakPoints and Stepping through the code. see http://www.cpearson.com/excel/Debug.htm
    Is that if there are two zeros in adjacent rows it misses the second one. The reason is that when the code did a Shift:=xlUp the RowCtr then skipped over that row. To fix that we can move the RowCtr back one with a "RowCtr = RowCtr -1 " statement. So this might work now..
    Please Login or Register  to view this content.
    Programmers who write clean code don't like the For Next Loop and they don't like the .Select type of statements as they slow things down. My intent from the above is to take you from the Macro Recorder to show you how to move to the next level.

    We need to have some good programmers write the above code in a more efficient method. You will see then count backwards from 130 to 15 using a "Step" parameter in the For Next loop so the Shift:=xlUp will work without the need to subtact 1 from the RowCtr. You will see them combine the .Select and Selection into a single line like:
    Please Login or Register  to view this content.
    You might even see them leave the Shift out completely.

    BTW - I believe you need to learn and almost memorize the following.
    Please Login or Register  to view this content.
    I like to use "Cells" instead of "Range" when writing code. I think I learned to always put the Row first and the Column second (kind of like (x,y) when graphing stuff in school).

    I hope this gives you a start in writing your own code.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  4. #4
    Registered User
    Join Date
    02-04-2011
    Location
    england
    MS-Off Ver
    Excel 2003
    Posts
    4

    Thumbs up Re: Deleting rows containing zero within specified range

    Wow! Thank you both for the speedy replies!

    Secondly, thanks for the great in depth explanation. I'm away from the computer now but I'll let you know how I get on with it.

    Since posting I did figure out a work around. I changed the formula in the range to return zeroes as ones and ones as "". This meant I could record a macro and select the cells containing a one by using Go to - Special - Formula (only numbers). Then proceeded to delete the rows and stop recording. It works but it's not very elegant, especially since the A column is a helper column in the first place!

    Anyway, thanks again and I look forward to trying out your code later on

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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