Code To Look For Number On Sheet 1 And Put Another Number Directly Below

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,748
Office Version
  1. 365
Platform
  1. Windows
I have sheet 2 with a list of numbers in column A. I need a code please that will look for these numbers on sheet 1 in column AD and put the number that is next to it in column B on sheet 2 in the cell directly below in column Ad on sheet 1. Thanks.


Excel 2010
AB
1
2TestTest1
3DataData1
Sheet2


Sheet 1 Before


Excel 2010
AD
2Test
3NYA
4NYA
5Test
6NLA
7Data
8NYA
9NYA
10NYA
11NYA
12Data
Sheet1


Sheet 1 After. I have highlighted the cells where the data has been added.


Excel 2010
AD
2Test
3Test1
4NYA
5Test
6Test1
7Data
8Data1
9NYA
10NYA
11NYA
12Data
13Data1
Sheet1


Thanks.
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG12Oct48
[COLOR="Navy"]Dim[/COLOR] Rng1 [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, Rng2 [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]With[/COLOR] Sheets("Sheet2")
    [COLOR="Navy"]Set[/COLOR] Rng2 = .Range(.Range("A1"), .Range("A" & Rows.Count).End(xlUp))
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]With[/COLOR] Sheets("Sheet1")
    [COLOR="Navy"]Set[/COLOR] Rng1 = .Range(.Range("AD1"), .Range("AD" & Rows.Count).End(xlUp))
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng2
    .Item(Dn.Value) = Dn.Offset(, 1).Value
[COLOR="Navy"]Next[/COLOR]
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng1
    [COLOR="Navy"]If[/COLOR] .exists(Dn.Value) [COLOR="Navy"]Then[/COLOR] Dn.Offset(1) = .Item(Dn.Value)
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Thanks Mick, but when I run it everything in column AD on sheet 1 disappears?
 
Upvote 0
Thanks got it working now. The file I was trying it on didn't have a header in row 1, so I put one there and it worked.
 
Upvote 0
Hi Mick. Could you tweak this a little further please. When the number is added below in AD, could a description that will be in column C on sheet 2 be added next to the added number in column AE on sheet 1 please. Thanks.
 
Upvote 0
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG13Oct00
[COLOR="Navy"]Dim[/COLOR] Rng1 [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, Rng2 [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]With[/COLOR] Sheets("Sheet2")
    [COLOR="Navy"]Set[/COLOR] Rng2 = .Range(.Range("A1"), .Range("A" & Rows.Count).End(xlUp))
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]With[/COLOR] Sheets("Sheet1")
    [COLOR="Navy"]Set[/COLOR] Rng1 = .Range(.Range("AD1"), .Range("AD" & Rows.Count).End(xlUp))
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng2
   [COLOR="Navy"]Set[/COLOR] .Item(Dn.Value) = Dn
[COLOR="Navy"]Next[/COLOR]
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng1
    [COLOR="Navy"]If[/COLOR] .Exists(Dn.Value) [COLOR="Navy"]Then[/COLOR]
        Dn.Offset(1).Value = .Item(Dn.Value).Offset(, 1).Value
        Dn.Offset(1, 1).Value = .Item(Dn.Value).Offset(, 2).Value
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Hi Mick, I need a code something along these lines. Firstly the data in column C on sheet 1 needs to match. On sheet 2 there will be numbers in column A and another next to it in column B and a description in column C. When the number is found in column AD on sheet 1 then the number needs to be changed ABOVE this time and only if there is either 'NYA', 'N/A' or 'NLA' there'

On the example below the data in C matches and there is NYA in column AD so the MCA019 on sheet 2 can go above along with the description in C. But on the bottom row it is just a singular row so that needs to be ignored. It is only if there are multiple rows with matching data in C.

Sheet 1 before code


Excel 2010
CADAE
585ASDB759 4004NYACalcium Battery
586ASDB759 4004MIB019Intel 4YR 95AH / 800SAE
587ASDB759 4008NYACalcium Battery
588ASDB759 4008MIB019Intel 4YR 95AH / 800SAE
589ASDB759 4011MIB019Intel 4YR 95AH / 800SAE
Sheet1


Sheet 1 after code


Excel 2010
CADAE
585ASDB759 4004MCA019Calcium 3YR 100AH / 760EN
586ASDB759 4004MIB019Intel 4YR 95AH / 800SAE
587ASDB759 4008MCA019Calcium 3YR 100AH / 760EN
588ASDB759 4008MIB019Intel 4YR 95AH / 800SAE
589ASDB759 4011MIB019Intel 4YR 95AH / 800SAE
Sheet1


sheet 2


Excel 2010
ABC
2MIB019MCA019Calcium 3YR 100AH / 760EN
Sheet2


Thanks Mick
 
Upvote 0

Forum statistics

Threads
1,215,506
Messages
6,125,197
Members
449,214
Latest member
mr_ordinaryboy

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