Results 1 to 11 of 11

second row in loop doesn't always delete. Please Help

Threaded View

  1. #1
    Registered User
    Join Date
    05-03-2017
    Location
    London, England
    MS-Off Ver
    2010-2016
    Posts
    4

    second row in loop doesn't always delete. Please Help

    I am writing a macro that checks to see if a cell, and the cell directly below it within the same column are the same. So for instance it will start with z2, then compare z2 to z3. Only if z2 and z3 are the same value , then the macro will take the sum of the two corresponding cells in a different column of their respective rows i.e. b2 + b3. Only if b2+b3 equals 0, then i want the macro to delete the rows 2 + 3, then proceed to check if z4 (or z3 if not previously erased) is the same as z5 (or z4 again depending upon scenario) and so forth until the column runs out. My problem is sometimes both rows get deleted - and other times only 1 of the two rows get deleted and i don't know why some are and some aren't.

    Code so far. Please be aware I am still a vba novice. Feel free to change my code however you may see fit.
    Sub CompareZ_Values()
    
    Dim firstIndex, secIndex, firstQ, secondQ As Integer
    firstIndex = 2
    secIndex = 3
    firstQ = 2
    secondQ = 3
    
    Do
        If Cells(firstIndex, 26).Value = Cells(secIndex, 26).Value Then 'check if the Z2 is equal to Z3, if it is move on into the loop
            If Cells(firstQ, 21).Value + Cells(secondQ, 21).Value = 0 Then 'check if sum U2 + U3 = 0, if it is move on
                Rows(firstQ).Select 'select first row to delete
                Selection.EntireRow.Delete
                Rows(firstQ).Select 'select second row to delete
                Selection.EntireRow.Delete 'this is the part where I know the root of the problem lies 
        End If
        End If
        firstIndex = firstIndex + 1
        secIndex = secIndex + 1
        firstQ = firstQ + 1
        secondQ = secondQ + 1 'this is me trying to tell the macro to move onto the next cell be it Z3 or Z4 depending on if Z3 gets erased or not
    
    Loop While Cells(firstIndex, 28).Value <> "" And Cells(secIndex, 28).Value <> "" 'checks to see if cells are NULL, if not NULL than initiate the loop
    
    End Sub
    EDIT: i cannot seem to upload a sample spreadsheet. I am trying to fix this and get one up asap.
    Last edited by johndoe15; 05-09-2017 at 09:47 PM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. help on a Do loop that doesn't work
    By g_liron in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-12-2015, 10:50 AM
  2. [SOLVED] Doesn't recognize the loop?
    By AllThingsIan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-13-2014, 04:34 AM
  3. [SOLVED] Do until loop that doesn't seem to end
    By Sc0tt1e in forum Excel Programming / VBA / Macros
    Replies: 31
    Last Post: 03-31-2014, 06:07 PM
  4. Replies: 7
    Last Post: 10-23-2012, 08:38 AM
  5. [SOLVED] For Each loop doesn't take every row into account
    By Tino XXL in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-15-2012, 07:54 AM
  6. Macro Loop Broken. Detects Max but doesn't continue loop
    By herchenbach in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-21-2011, 12:17 PM
  7. My loop doesn't work why not
    By ljh66 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-06-2007, 01:02 PM

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