Copying and Pasting cells by matching item numbers

ericpny2

New Member
Joined
Jun 4, 2011
Messages
33
so i have a worksheet that has a 4 digit item number and a 5 digit item number (separate cells). I have thousands of these items with the item numbers. I have tried to write a code that will go through the "old" numbers (the larger list) and copy them to the new list. the new list is a requested list of a couple hundred from the larger list. i want to copy the description of these items, not the item numbers

what i have tried to write is a for loop for the new list with a for loop inside of that with the old list. the inside for loop will run through the old list adn search each row until it can match up the new item number with the old item number. i then want it to paste the description into the cell next to the new list. so the inner loop will run thru all the old list and the outer loop will run through the new list

if i am not doing this efficiently then please let me know of a better way. i will attach the code below. let me know if you have any questions


Sub Populate()
Dim fourdigitnew As Double, fivedigitnew As Double, fourdigitold As Double, fivedigitold As Double
Dim ws As Worksheet
Dim primaveraruns As Long, i As Long, vdotruns As Long, n As Long
Set ws = ActiveSheet
Dim descriptionold As String, descriptionnew As String

primaveraruns = Application.InputBox("How Many P Runs? ")
vdotruns = Application.InputBox("How Many vdot Runs?")

For i = 1 To primaveraruns
fourdigitnew = ws.Cells(2 + i, 1)
fivedigitnew = ws.Cells(2 + i, 2)
descriptionnew = ws.Cells(2 + 1, 4)

For n = 1 To vdotruns

descriptionold = ws.Cells(2 + i, 9)
fourdigitold = ws.Cells(2 + i, 7)
fivedigitold = ws.Cells(2 + i, 8)

If fourdigitold = fourdigitnew And fivedigitold = fivedigitnew Then
descriptionold = descriptionnew
Else: descriptionold = ""
End If

Next n
Next i
End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Here is the most recent code that i have

Sub Populate()
Dim fourdigitnew As Double, fivedigitnew As Double, fourdigitold As Double, fivedigitold As Double
Dim ws As Worksheet
Dim primaveraruns As Long, i As Long, vdotruns As Long, n As Long
Set ws = ActiveSheet
Dim descriptionold As String, descriptionnew As String
primaveraruns = Application.InputBox("How Many P Runs? ")
vdotruns = Application.InputBox("How Many vdot Runs?")
For i = 1 To primaveraruns
fourdigitnew = ws.Cells(2 + i, 1)
fivedigitnew = ws.Cells(2 + i, 2)

For n = 1 To vdotruns

descriptionold = ws.Cells(2 + n, 9)
fourdigitold = ws.Cells(2 + n, 7)
fivedigitold = ws.Cells(2 + n, 8)
descriptionnew = ws.Cells(2 + i, 4)

If fourdigitold = fourdigitnew And fivedigitold = fivedigitnew Then
descriptionnew = ws.Cells(2 + n, 9)
End If

Next n
Next i
End Sub
 
Upvote 0
I have now limited the problem to the cut and pasting part:

i have changed the If statement to:

If fourdigitold = fourdigitnew And fivedigitold = fivedigitnew Then
Range("2+n,9").Select
Selection.Cut
Range("2+i,4").Select
ActiveSheet.Paste
End If

but it is coming with an error on the line: Range("2+n,9").Select


Im pretty sure once this is fixed it should run correctly

thanks
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,827
Members
452,946
Latest member
JoseDavid

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