If statement

jaypatel

Active Member
Joined
Nov 25, 2002
Messages
389
Hi,

I have 2 columns A and B of course.....

I need a script that would do the following:

If a3=a2 and b3=b2 then delete row
if a3=a2 and b3>b2 then delete row b2
if a3>a2 then keep row a2..... in a kind of loop effect.

Any help would be appreciated.

Regards

Jay
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Try this and let me know if you have any trouble:

Code:
    Dim r As Long

    r = 2
    Do While Cells(r, "A").Value <> ""
        If Cells(r, "A").Value = Cells(r - 1, "A").Value _
        And Cells(r, "B").Value = Cells(r - 1, "B").Value Then
            'This will delete the lower of the 2 rows, not sure I understood you here!
            Rows(r).Delete
            r = r - 1
        ElseIf Cells(r, "A").Value = Cells(r - 1, "A").Value _
        And Cells(r, "B").Value > Cells(r - 1, "B").Value Then
            Rows(r - 1).Delete
            r = r - 1
        ElseIf Cells(r, "A").Value > Cells(r - 1, "A").Value Then
            Rows(r).Delete
            r = r - 1
        End If
        r = r + 1
    Loop
 
Upvote 0
Hi Rob,

Just tried it and it deleted all the rows..... thanks for trying.....

If you wish to clarify anything, i can send you the spreadsheet with a before and after.....

Regards

Jay
 
Upvote 0
I guess you are tring to remove duplicuts. I just use a quick and dirty
=if(a2=a1,1,2)
Then just autofilter the 2s and delete all the rows.
 
Upvote 0
Without seeing your data I can only guess where this is going wrong. Have a look at the If, ElseIf statements and make sure the logic matches your requirements, I may have misunderstood you. Check that my code deletes the correct row, should we delete r or r-1 etc.

Also, try stepping through the code with the F8 key, the error may be obvious when you see things step by step.

I have to leave soon but will check this thread tomorrow.
 
Upvote 0
Perhaps:

Code:
Sub test()
Dim x As Long, y As Long
y = Range("A65536").End(xlUp).Row
For x = y To 3 Step -2
    If Range("A" & x) = Range("A" & x - 1) Then
        If Range("B" & x) = Range("B" & x - 1) Then
            Range("A" & x).EntireRow.Delete
        ElseIf Range("B" & x) > Range("B" & x - 1) Then
            Range("A" & x - 1).EntireRow.Delete
        End If
    End If
Next x
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,553
Messages
6,114,279
Members
448,562
Latest member
Flashbond

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top