Replacing Values

rholdren

Board Regular
Joined
Aug 25, 2016
Messages
140
Office Version
  1. 365
  2. 2019
A friend of mine asked me if I could write something that would use the names on sheet two in column be to replace the names on sheet 1 in column a. The actual sheet has about 47K lines of data and will grow as the year grows. What is listed below is completely made up. I've looked at the SWITCH and MATCH and don't believe those to be heading in the right direction. What direction should I be looking? To be hones I'm not even certain where to post this. I've tried searching using a variety of terms and phrases and I just couldn't find the right terms. So, I thought I would start fresh this morning and post, then apologize for probably not finding what I should have and also placing this in the correct location.


1630326284997.png
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
If the names are already entered as text in sheet 1 then a formula will not work. A formula can only show a result in the cell where it is entered, it can not send information or make changes to existing entries elsewhere.

If sheet1 column A already contains formulas then you should be able to edit the formula to do what is needed, we would need to see the existing formula to advise further.

Most likely you will need to use vba to emulate find and replace, cycling through the names in sheet 2 with a loop. (this would be the same as using Ctrl h to make changes but would be automated).
 
Upvote 0
If the names are already entered as text in sheet 1 then a formula will not work. A formula can only show a result in the cell where it is entered, it can not send information or make changes to existing entries elsewhere.

If sheet1 column A already contains formulas then you should be able to edit the formula to do what is needed, we would need to see the existing formula to advise further.

Most likely you will need to use vba to emulate find and replace, cycling through the names in sheet 2 with a loop. (this would be the same as using Ctrl h to make changes but would be automated).
Neither worksheet has a formula in them. They were a slight representation. The exact file has personal info on it so I just made a mini-replica. I'm okay with the looping but I'm not sure how to say find this value in column a on sheet2 and replace it on sheet1 with the value in column b on sheet2.
 
Upvote 0
Give this a try with a copy of the workbook.

VBA Code:
Sub ReplaceNames()
  Dim a As Variant
  Dim i As Long
  
  a = Sheets("Sheet2").Range("A2", Sheets("Sheet2").Range("B" & Rows.Count).End(xlUp)).Value2
  Application.ScreenUpdating = False
  With Sheets("Sheet1")
    For i = 1 To UBound(a)
      .Columns("A").Replace What:=a(i, 1), Replacement:=a(i, 2), LookAt:=xlWhole, MatchCase:=False
    Next i
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Give this a try with a copy of the workbook.

VBA Code:
Sub ReplaceNames()
  Dim a As Variant
  Dim i As Long
 
  a = Sheets("Sheet2").Range("A2", Sheets("Sheet2").Range("B" & Rows.Count).End(xlUp)).Value2
  Application.ScreenUpdating = False
  With Sheets("Sheet1")
    For i = 1 To UBound(a)
      .Columns("A").Replace What:=a(i, 1), Replacement:=a(i, 2), LookAt:=xlWhole, MatchCase:=False
    Next i
  End With
  Application.ScreenUpdating = True
End Sub

Peter_SSs Thank you again so much. In trying to understand this the .Value2 does that refer to the matching value in column b? Also still trying to get comfortable with Unbound. It's ironic that I have taken a lot of the classes online (and learned a lot) but I learn so much more from the great minds on this BB. Your help is grealty appreciated.​

 
Upvote 0
In trying to understand this the .Value2 does that refer to the matching value in column b?
No, just all the values from A2 down to the bottom of the data in column B are read into the array 'a'. That is, values from both columns.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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