Help with Marco - how do I find and delete the whole row if value exists in both columns

crag8119

New Member
Joined
Mar 25, 2014
Messages
38
Hi,

In Columns O & P I have values like below:

[TABLE="width: 128"]
<colgroup><col><col></colgroup><tbody>[TR]
[TD="align: right"]0[/TD]
[TD]R1177092[/TD]
[/TR]
[TR]
[TD="align: right"]0[/TD]
[TD]R1177093[/TD]
[/TR]
[TR]
[TD="align: right"]0[/TD]
[TD]R1177227[/TD]
[/TR]
[TR]
[TD]R1178634[/TD]
[TD]R1178511[/TD]
[/TR]
[TR]
[TD="align: right"]0[/TD]
[TD]R1178185[/TD]
[/TR]
[TR]
[TD="align: right"]0[/TD]
[TD]R1178634[/TD]
[/TR]
[TR]
[TD] R1179555[/TD]
[TD="align: right"]0[/TD]
[/TR]
</tbody>[/TABLE]


I need a Marco to find and delete the whole row , if the same values exists in both columns.

For example: R1178634 exists in column O and on a different row column P. I want to delete the whole rows.

Delete row 4 in Columns O
and Delete row 6 in Columns P as value R1178634 exists in both columns.

Thanks
 
if the same value (R123456789) appears 10 times in column "O" and 6 times in column "P" what should remain in both columns?

if the value appears 6 times in both columns what should remain in both columns?

if the value appears 6 times in column "P" (and not at all in "O") what should remain in both columns?

thanks
 
Upvote 0

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
if the same value (R123456789) appears 10 times in column "O" and 6 times in column "P" what should remain in both columns?

if the value appears 6 times in both columns what should remain in both columns?

if the value appears 6 times in column "P" (and not at all in "O") what should remain in both columns?

thanks

Hi ,

Thank you again.

I am only delete matches in both columns, the value has to exist in O and P to be deleted.

if the same value (R123456789) appears 10 times in column "O" and 6 times in column "P" what should remain in both columns?
A: Remove 6 from O and 6 from P , leave 4 in O

if the value appears 6 times in both columns what should remain in both columns?
A: 0 - Remove all lines

if the value appears 6 times in column "P" (and not at all in "O") what should remain in both columns?
A: 6 will remain in P as not match in O.

If there was 5 in P and 4 in O , only 4 from both would be deleted. 1 would be left in P.

Hope that makes sense
 
Upvote 0
Alternative;
Code:
Sub TEST()    
    Dim son As Integer, huc As Range, sil As Range
    With CreateObject("Scripting.Dictionary")
        son = Cells(Rows.Count, "O").End(3).Row
        If son > 6000 Then son = 6000
        For Each huc In Range("O30:O" & son).Cells
            If huc.Value <> "" And huc.Value <> 0 Then .Item(huc.Value) = huc.Row
        Next huc
        son = Cells(Rows.Count, "P").End(3).Row
        If son > 6000 Then son = 6000
        For Each huc In Range("P30:P" & son)
            If huc.Value <> "" And huc.Value <> 0 Then
                If .exists(huc.Value) Then
                    If sil Is Nothing Then
                        Set sil = Union(Rows(huc.Row), Rows(.Item(huc.Value)))
                    Else
                        Set sil = Union(sil, Rows(huc.Row), Rows(.Item(huc.Value)))
                    End If
                End If
            End If
        Next huc
    End With
    If Not sil Is Nothing Then sil.Delete
End Sub
 
Last edited:
Upvote 0
try this

Code:
Sub DeleteRows()
    Dim c1  As Range, c2 As Range, Rng As Range, r As Long
    With ActiveSheet
        Set Rng = .Range("P30:P6000")
        For r = 6000 To 30 Step -1
            Set c1 = .Cells(r, "O")
            Set c2 = Nothing
            If c1 <> "" And c1 <> 0 Then
                On Error Resume Next
                Set c2 = Rng.Find(c1, LookIn:=xlValues, lookat:=xlWhole)
                On Error GoTo 0
                If Not c2 Is Nothing Then Union(c1, c2).EntireRow.Delete
            End If
        Next r
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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