Match and replace cell from another sheet

gravy679

New Member
Joined
May 10, 2008
Messages
4
Question: I have two sheets named Sheet1 and Sheet2. Sheets looks like this:

Sheet1
A B
1 John Smith
2 Jane Doe
3 Joe Smo
4 Jackie Chan

Sheet2
A
John Smith
Joe Smo
Joe Smo
Joe Smo
Jane Doe
Bruce Lee

Here is the logic: If any cell in Sheet2 Column A = Any cell in Sheet1 Column B, change sheet2 column A cell value to sheet1 column A value (id).

Thanks...
 
Code:
Sub Test1()
Application.ScreenUpdating = False
Dim cell As Range, varFind As Variant
With Sheets("Sheet1")
For Each cell In Sheets("Sheet2").Columns(1).SpecialCells(2)
Set varFind = .Columns(2).Find(What:=cell.Value, LookIn:=xlFormulas, LookAt:=xlWhole)
If Not varFind Is Nothing Then cell.Value = .Cells(varFind.Row, 1).Value
Set varFind = Nothing
Next cell
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Another approach. It seems like sheet 1 has the list of unique items and sheet 2 the multiple entries. Whereas Tom has looped through every cell in sheet 2, and that is what you asked, this loops through every cell in sheet 1 and maybe handy too. HTH, Fazza

Code:
Sub test()
  Application.ScreenUpdating = False
  Dim cel As Range
  
  For Each cel In Worksheets("Sheet1").Range("B1").CurrentRegion.Columns(2).Cells
    Worksheets("Sheet2").Columns(1).Replace What:=cel.Value, Replacement:=cel.Offset(, -1).Value, _
    LookAt:=xlWhole, SearchOrder:=xlByColumns, MatchCase:=True
  Next cel
End Sub
 
Upvote 0
Tom Urtis said:
Code:
Sub Test1()
Application.ScreenUpdating = False
Dim cell As Range, varFind As Variant
With Sheets("Sheet1")
For Each cell In Sheets("Sheet2").Columns(1).SpecialCells(2)
Set varFind = .Columns(2).Find(What:=cell.Value, LookIn:=xlFormulas, LookAt:=xlWhole)
If Not varFind Is Nothing Then cell.Value = .Cells(varFind.Row, 1).Value
Set varFind = Nothing
Next cell
End With
Application.ScreenUpdating = True
End Sub
Why loop when it is not required?
Code:
Sub test()
Dim x As String, y As String
With Sheets("sheet1")
    x = .Range("b1", .Range("b" & Rows.Count).End(xlUp)).Resize(,2).Address
End With
With Sheets("sheet2")
    With .Range("a2", .Range("a" & Rows.Count).End(xlUp)).Offset(,1)
        y = .Offset(,-1).Address
        .Value = Evaluate("if(" & y & "<>"""",vlookup(" & y & ","sheet1!" & x & ",2,false),"""")")
        On Error Resume Next
        .SpecialCells(2,16).ClearContents
    End With
End With
End Sub
 
Last edited:
Upvote 0
Why loop when it is not required?
Not every loop is a bad loop.

Did you test your code, if so what results did you get, when I tested it on the data per the original post there was no change to anything on Sheet2.
 
Upvote 0
It should after I have last edited my code.

It is not edited..
should be
Code:
        .Value = Evaluate("if(" & y & "<>"""",vlookup(" & y & ",sheet1!" & x & ",2,false),"""")")
 
Last edited:
Upvote 0
Test it again or for the first time, it still does not work for 2 reasons, one, it places zeros in column B of sheet2, and two, it should replace what is in column A of Sheet2.

Risky also because you assumed column B would be available when it might be occupied by other data, where the loop would not have disturbed the original data, which is one reason why not every loop is a bad loop.

Your method might be better, but I cannot get it to work.
 
Upvote 0
Reason I posted the code was, you said
Code:
why start off with a loop if you don't need one.
in other thread, and I didn't expected your comment like
Not every loop is a bad loop
 
Upvote 0
Well, like I said, not every loop is a bad loop; sometimes they are more efficient than not looping, while in other cases, loops are neither needed nor advisable but are suggested by others when it makes more sense to not have them. That's the way it goes.

I also see some people post strong but misguided apocalyptic warnings against Select and SendKeys, when both of those methods have their place and in some cases make more sense to use them than not use them. That's the way that goes too.
 
Upvote 0

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