Help With Find & Copy Code

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,786
Office Version
  1. 365
Platform
  1. Windows
I was given the code below that looks at column A on sheet 1 and then looks for that value in column 28 on sheet 2. When a match is found it copies what is in column B on sheet 1 into column 29 on sheet 2.

I have 2 problems, the first is that when it finds a match it adds the data next to it fine but when that number occurs later down in the Sheet it doesnt do anything. Also it takes forever to run and sometimes stops responding as it has to look at 30-40,000 rows. can anyone help please.

Thanks.

Code:
Sub MatchAndCopy1()
Sheets(1).Activate
a = 1
Do While Cells(a, 1) <> ""
x = Cells(a, 1).Text
B = 1
Do While Sheets(2).Cells(B, 28) <> ""
If x = Sheets(2).Cells(B, 28).Text Then
Sheets(2).Cells(B, 29) = Cells(a, 2).Text
GoTo nxt
End If
B = B + 1
Loop
nxt:
a = a + 1
Loop
Sheets(2).Activate
End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
This sounds like you need to put more focus on sheet 2.

Couldn't you use a vlookup formula in column 29 on sheet 2 to achieve this?

in row 2:

=vlookup(AB2,sheet1!A:B,2,0)

and copy down

You might have to change the name of the sheet to fit your setup.

To clarify

I have 2 problems, the first is that when it finds a match it adds the data next to it fine but when that number occurs later down in the Sheet it doesnt do anything.
which dataset are we talking about here, sheet1, or sheet2?
 
Upvote 0

Forum statistics

Threads
1,224,585
Messages
6,179,700
Members
452,938
Latest member
babeneker

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