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.
 
Hi Worf, what part do I need to change so it does the cells directly above rather than below as it does now? Thanks.
 
Upvote 0

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Had chance to look at this yet Worf? I've tried changing some offsets but it doesn't work.
 
Upvote 0
New version:

Code:
Sub MG13Oct00_Worf()
Dim Rng1 As Range, Dn As Range, Rng2 As Range, sm As Worksheet
With Sheets("Sheet2")
    Set Rng2 = .Range(.[a1], .Range("A" & Rows.Count).End(xlUp))
End With
With Sheets("Sheet1")
    Set Rng1 = .Range(.[AD1], .Range("AD" & Rows.Count).End(xlUp))
End With
If Not SheetExists("summary") Then
    Sheets.Add , Sheets(Sheets.Count)
    Sheets(Sheets.Count).Name = "Summary"
End If
Set sm = Sheets("summary")
sm.[a:a].ClearContents
sm.[a1] = "Addresses are listed below:"
With CreateObject("scripting.dictionary")
    .CompareMode = vbTextCompare
    For Each Dn In Rng2
       Set .Item(Dn.Value) = Dn
    Next
    Sheets("sheet2").Activate
    For Each Dn In Rng1             'column AD
        If .Exists(Dn.Value) Then
            If Dn.Offset(-1) = "NLA" Or Dn.Offset(-1) = "NYA" Or Dn.Offset(-1) = "N/A" Then
                Dn.Offset(-1) = .Item(Dn.Value).Offset(, 1)     ' negative offset is the row above
                Dn.Offset(-1, 1) = .Item(Dn.Value).Offset(, 2)  ' offset(row,column)
            Else
                sm.Cells(sm.Range("a" & Rows.Count).End(xlUp).Row + 1, 1) = Dn.Offset(-1).Address
                Dn.Offset(-1).Interior.Color = RGB(170, 120, 160)
            End If
        End If
    Next
End With
End Sub
 
Upvote 0
Thanks worf I will try at work in the morning.
 
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

Sorry its been a while since you did this code Mick. You can probably tell what it does by looking at it but if possible I would like a line of code added so that when a value is added below it colours the rows? Thanks
 
Upvote 0
Possibly, untested:-
Code:
For Each Dn In Rng1
    If .Exists(Dn.Value) Then
        Dn.Offset(1).Value = .Item(Dn.Value).Offset(, 1).Value
        
       [COLOR="#FF0000"][B]Dn.offset(1).Entirerow.interior.color = vbyellow
[/B][/COLOR]        
       Dn.Offset(1, 1).Value = .Item(Dn.Value).Offset(, 2).Value
    End If
Next Dn
 
Upvote 0
Thanks but it coloured the entire sheet?
 
Upvote 0
When I run the code on your data in Post#1 it returns the same results as shown in post#1 except the whole row is highlighted.
What part of that doesn't work for you.???
 
Upvote 0
Sorry I got confused on code. You did 2 for me in this thread the one I need is the code in post 3 where it does not add description like the other.
 
Upvote 0
Ref:- Post#3 Code
Modify Post#3 code with lines in red below:-
Code:
For Each Dn In Rng1
   
  [COLOR="#FF0000"][B] If .Exists(Dn.Value) Then
        Dn.Offset(1) = .Item(Dn.Value)
        Dn.Offset(1).EntireRow.Interior.Color = vbYellow
   End If
[/B][/COLOR]
Next Dn
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,506
Messages
6,125,189
Members
449,213
Latest member
Kirbito

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