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.
 
Please test this:

Code:
Sub MG13Oct00_Worf()
Dim Rng1 As Range, Dn As Range, Rng2 As Range
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
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
        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)
                Dn.Offset(1, 1) = .Item(Dn.Value).Offset(, 2)
            Else
                Sheets("sheet3").Cells(Sheets("sheet3").Range("a" & _
                Rows.Count).End(xlUp).Row + 1, 1) = Dn.Offset(1)
            End If
        End If
    Next
End With
End Sub
 
Upvote 0

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Thanks worf, it mainly worked. The only problem was sheet 3. It didn't list the cell reference where the data couldn't be put below because there was something other than NYA, NLA or N/A. It seemed that it listed what was below, rather than saying for example 'AD23 couldn't be changed because something else was there'.
 
Upvote 0
Also please worf I have just realised some files I will use this on may have data already on sheet 3. Could a sheet be created please called summary?
 
Upvote 0
New version:

Code:
Function SheetExists(s$) As Boolean
Dim x As Object
SheetExists = False
On Error Resume Next
Set x = ActiveWorkbook.Sheets(s)
If Err.Number = 0 Then SheetExists = True
On Error GoTo 0
End Function


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
        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)
                Dn.Offset(1, 1) = .Item(Dn.Value).Offset(, 2)
            Else
                sm.Cells(sm.Range("a" & Rows.count).End(xlUp).Row + 1, 1) = Dn.Offset(1).Address
            End If
        End If
    Next
End With
End Sub
 
Upvote 0
Thanks worf, works perfect. Thanks for your time.
 
Upvote 0
Hi again worf. Sorry to put you to more trouble but as well as a summary sheet of cells that could not be changed, could each row be highlighted in a colour also please?
 
Upvote 0
See if this highlights the correct data:

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
        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)
                Dn.Offset(1, 1) = .Item(Dn.Value).Offset(, 2)
            Else
                sm.Cells(sm.Range("a" & Rows.Count).End(xlUp).Row + 1, 1) = Dn.Offset(1).Address
                Dn.Offset(1).Interior.Color = RGB(160, 110, 150)
            End If
        End If
    Next
End With
End Sub
 
Upvote 0
Thanks worf it does appear to work, however could it highlight the entire row rather than just the cell please.
 
Upvote 0
Use this instead:

Code:
Dn.Offset(1).EntireRow.Interior.Color = RGB(170, 120, 160)
 
Upvote 0

Forum statistics

Threads
1,215,012
Messages
6,122,682
Members
449,091
Latest member
peppernaut

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