PC Review


Reply
Thread Tools Rate Thread

Compare 2 Ranges, Copy/Paste Row on Match

 
 
Dan R.
Guest
Posts: n/a
 
      23rd May 2007
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

 
Reply With Quote
 
 
 
 
=?Utf-8?B?Sm9lbA==?=
Guest
Posts: n/a
 
      23rd May 2007
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

"Dan R." wrote:

> 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
>
>

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Compare 2 Ranges, Copy/Paste Row on Match Dan R. Microsoft Excel Programming 0 23rd May 2007 08:34 PM
Re: Compare 2 Ranges, Copy/Paste Row on Match Dan R. Microsoft Excel Programming 0 23rd May 2007 08:29 PM
Re: Compare 2 Ranges, Copy/Paste Row on Match Dan R. Microsoft Excel Programming 0 23rd May 2007 08:04 PM
Re: Compare 2 Ranges, Copy/Paste Row on Match Dan R. Microsoft Excel Programming 0 23rd May 2007 07:55 PM
Re: Compare 2 Ranges, Copy/Paste Row on Match Dan R. Microsoft Excel Programming 0 23rd May 2007 07:52 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:01 AM.