Delete entire row if value found on the other worksheet

murrish

New Member
Joined
Jun 18, 2018
Messages
4
Hi!
I have 2 worksheets with extensive data from patient registry. What I want to do is go through worksheet2 row by row only in a single column(A). For each cell in every row in that specific column(A) I want to find that cell in worksheet1(which is 100% there, also in first column(A)) and delete the entire row. My code seems to almost work, problem is that it is missing a few ones. Why is that? Excuse my poorly written code this is not really my field. Thx in advance!

Code:
Sub CheckA()
Dim LR As Long, i As Long, abc As Long, d As Variant
With Sheets("Sheet3")
    LR = .Range("A" & Rows.Count).End(xlUp).Row
    
    For i = LR To 1 Step -1
        
     abc = Worksheets("sheet3").Range("A" & i).Value
   
          Set d = Worksheets("sheet2").Range("A:A").Find(abc)
          If Not d Is Nothing Then
           d.EntireRow.Delete
            Else
            End If
      Next i
    
End With
End Sub
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Welcome to the Board!

Nothing obvious stands out, it looks like your code should work.

When you say "missing a few ones", what exactly do you mean?
If it deleting rows it shouldn't be, or not deleting rows that is should be?

Can you provide some simple examples of what the data in column A on each sheet looks like?
 
Upvote 0
Hi and thank you! It is deleting a row correctly. When I say "missing a few" I mean that theres over 1200 rows on sheet2. All of these gets deleted correctly but a few(under 10) don't. These that are not deleted can still be found on sheet2 in column A just like those that were correctly deleted. I can obviously delete these manually but in the future there is going to be more data.
Example data from column could be any number from 500-90000.
 
Upvote 0
I am unable to edit my post so sorry for double posting. When I say incorrectly deleting I mean that its not doing it at all.
 
Upvote 0
Are there duplicate values in column A of Sheet2?
 
Upvote 0
Also note that the way you set up your Find, it will delete partial matches.
So, if you are looking for "32", and it finds "327", it will delete that!

If you want an exact match, use:
Code:
Set d = Worksheets("sheet2").Range("A:A").Find(abc[COLOR=#ff0000], , , xlWhole[/COLOR])

See this for all the arguments associated with FIND: https://msdn.microsoft.com/en-us/vba/excel-vba/articles/range-find-method-excel
 
Last edited:
Upvote 0
Thank you everyone!
Problem solved. I added the xwhole part and it worked. The incorrect ones were always the same and there were no doublets of them on "sheet3". 20521, 46540, 46918, 46920, 40806 were the ones if anyone is interested.
 
Upvote 0

Forum statistics

Threads
1,223,980
Messages
6,175,764
Members
452,668
Latest member
mrider123

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