+ Reply to Thread
Results 1 to 7 of 7

issues about deleting zero rows

  1. #1
    Registered User
    Join Date
    10-31-2014
    Location
    canada
    MS-Off Ver
    2007
    Posts
    64

    issues about deleting zero rows

    Hi,

    I need vba code to delete those entire rows with column F equal to zero. First I want to delete those rows that have zero in column F in "Section 1" (Rows 5 to Row 21), and then get the total in row "Section 1 Sub-total" after deleting. And then do the same thing for section 2 and section 3. I've developed code for deleting, but it works fine until row 11. I use debug.print to show the results I get for each run. Here are my results. The range has total 17 rows, so it gets 17 0, 16 0, 15, 0.....When it runs to row 15 (row 11 in my defined range), I get the result 11 175. This is the result based on entire sheet, not my defined range. Can anyone help on this? I've attached my file.

    17
    0
    16
    0
    15
    0
    14
    0
    13
    0
    12
    0
    11
    175
    10
    165

    Please Login or Register  to view this content.
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,525

    Re: issues about deleting zero rows

    Hi pmchris,

    This line...

    Rng.Rows.Count

    ...returns 17 so the code works from Row 17 not 21. You need use variables to determine the first and last row of the Rng variable and then loop backwards through those like so:

    Please Login or Register  to view this content.
    Or you just use this so you don't have to keep setting the Rng variable:

    Please Login or Register  to view this content.
    Regards,

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  3. #3
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,528

    Re: issues about deleting zero rows

    In "Section 2" you have no data. Should "Section 2" be totally deleted from A25 to A56?
    (Column F has no value in the row that has "Section 2" in Column A)

    In see that in the meantime Robert has supplied you with an answer.

  4. #4
    Registered User
    Join Date
    10-31-2014
    Location
    canada
    MS-Off Ver
    2007
    Posts
    64

    Re: issues about deleting zero rows

    Hi Robert,

    Much appreciate for your explanations and 2 codes. Both codes work perfectly. The first code is straightforward and I can understand. For the 2nd code, what Range("F" & i) does? and how can I interpret If Len(ws.Range("F" & i)) > 0 And ws.Range("F" & i) = 0 Then or what is the logical behind this sentence?

  5. #5
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,525

    Re: issues about deleting zero rows

    what Range("F" & i) does?
    It is checking each row i.e. i in Col. F

    how can I interpret If Len(ws.Range("F" & i)) > 0 And ws.Range("F" & i) = 0 Then or what is the logical behind this sentence?
    If there is something in Col. F for Row i and that value equals zero then delete the row. I came up with the logic from your posted example.

    Hope that helps,

    Robert

  6. #6
    Registered User
    Join Date
    10-31-2014
    Location
    canada
    MS-Off Ver
    2007
    Posts
    64

    Re: issues about deleting zero rows

    Got it. Thanks again, Robert.

  7. #7
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,525

    Re: issues about deleting zero rows

    You're welcome. Thanks for marking the thread as solved and 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. Inserting/Deleting Rows Causes Issues with Conditional Formatting Macro
    By lashellr in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-05-2014, 01:25 PM
  2. Replies: 2
    Last Post: 08-20-2013, 07:52 AM
  3. Changing code from deleting rows to cut/paste rows into another sheet and delete blank row
    By kmarshall6576 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-18-2013, 01:54 AM
  4. [SOLVED] Deleting part of a column without deleting whole rows.
    By dstrdOne in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-30-2013, 11:42 AM
  5. [SOLVED] Issues deleting a Row in VBA
    By bigman8424 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-27-2013, 12:46 AM
  6. VBA- deleting rows which contain cells that meet criteria, but skip alpha rows
    By cheapkid1 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-30-2012, 08:49 AM
  7. Deleting colored text and formatting issues-
    By Biased Historian in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 12-10-2010, 09:33 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