Compare 2 Ranges, Copy/Paste Row on Match

  • Thread starter Thread starter Dan R.
  • Start date Start date
D

Dan R.

I'm trying to compare the values in 2 ranges and if found, copy the
entire row to a new worksheet.

This is what I've come up with but it's not working:
'----

Set ws = Worksheets.Add

count = 1

For Each i In rng1
For Each x In rng2
If i.Value = x.Value Then
i.EntireRow.Copy ws.Rows(count)
count = count + 1
End If
Next x
Next i

'---

Thanks,
-- Dan
 
Your code is comparing every value in rng1 with every value in rng2. I think
you only want to compare items in same row

For Each i In rng1
If i.Value = i.offset(rowoffset:= 0, columnoffset:= 6).Value Then
i.EntireRow.Copy ws.Rows(count)
count = count + 1
End If
Next i


or something like this

for rowcount = 1 to Lastrow

If cells(rowcount, "i") = cells(rowcount,"x") then

cells(rowcount, "i").EntireRow.Copy ws.Rows(count)

end if


next rowcount
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top