Do Until IsEmpty Looping Issue

Marmaduke_88

New Member
Joined
Feb 18, 2012
Messages
26
Excel 2003. I believe I've got everything working in the code up until:
Code:
If rnGx = rnGy Then
It loops once and then "Run-time error '424': Object Required" for the code above.

Code:
Sub test1()
    Dim rnGx As Range
    Dim rnGy As Range
    Dim i As Long
    Dim xNum As Integer
    Dim yNum As Integer
    i = 2
    Set rnGx = Cells(i, 2)
    Set rnGy = rnGx.Offset(1, 0)
    xNum = rnGx.Offset(0, 2)
    yNum = rnGy.Offset(0, 2)
    With Sheet1
        Do Until IsEmpty(ActiveCell)
            If rnGx = rnGy Then
                rnGx.Offset(0, 2) = (xNum + yNum)
                rnGy.EntireRow.Delete
                i = (i + 1)
                Else
                i = (i + 1)
            End If
        Loop
    End With
End Sub
In case I'm way off, I'm trying to get the code to do the following:
  • look for identical entries in column b
  • where duplicates exist, sum the corresponding column d values into one of the existing columns
  • delete the remaining duplicate
  • do this until there are no matches

Column b is sorted ascending, so any matches should be next to each other. as I'm writing this, though - I realize that if there are triplicates or more in column B, this would need to be run more than once. Any suggestions?
 

cstimart

Well-known Member
Joined
Feb 25, 2010
Messages
1,180
Are you looking for something like this?

Code:
Sub test2()
Dim LR As Long
Dim rng As Range
LR = Cells(Rows.Count, 2).End(xlUp).Offset(-1).Row
    For Each rng In [B2].Resize(LR)
        If rng = rng.Offset(-1) Then
            rng.Offset(, 2) = rng.Offset(, 2) + rng.Offset(-1, 2)
            rng.Value = vbNullString
        End If
    Next rng
Columns(2).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub
Your code will run forever, as no values change within the "Loop". ;)
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,362
Office Version
365
Platform
Windows
You could be deleting rnGy on the first iteration.

So on the 2nd it doesn't exist.
 

Domski

Well-known Member
Joined
Jan 18, 2005
Messages
7,292
Try:

Code:
Dim lngLoopRow

For lngLoopRow = Range("B" & Rows.Count).End(xlUp).Row To 3 Step -1
    If Range("B" & lngLoopRow) = Range("B" & lngLoopRow - 1) Then 
        Range("D" & lngLoopRow - 1) = Range("D" & lngLoopRow - 1) + Range("D" & lngLoopRow)
        Rows(lngLoopRow).Delete
    End If
Next lngLoopRow
Dom
 

Marmaduke_88

New Member
Joined
Feb 18, 2012
Messages
26
thanks for the quick replies. danny I tried adding .value but it doesn't accept it (I'm thinking because they're ranges?).

cstimart, doesn't i = i+1 change the value in my loop? also, the code you provided doesn't seem to sum column d as it should.
 

cstimart

Well-known Member
Joined
Feb 25, 2010
Messages
1,180
thanks for the quick replies. danny I tried adding .value but it doesn't accept it (I'm thinking because they're ranges?).

cstimart, doesn't i = i+1 change the value in my loop? also, the code you provided doesn't seem to sum column d as it should.
The i = i + 1 is only increasing the value of i. You're not using the 'i' to change the cell(s) being a compared within the Do/Loop.

There was a glitch in my code....which is now fixed

Code:
Sub test2()
Dim LR As Long
Dim rng As Range
LR = Cells(Rows.Count, 2).End(xlUp).Offset(-1).Row
    For Each rng In [B2].Resize(LR)
        If rng = rng.Offset(-1) Then
            rng.Offset(, 2) = rng.Offset(, 2) + rng.Offset(-1, 2)
            rng.Offset(-1) = vbNullString
        End If
    Next rng
Columns(2).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub
 

Forum statistics

Threads
1,082,346
Messages
5,364,822
Members
400,814
Latest member
gangstar67

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top