yousufj567
New Member
- Joined
- Jul 6, 2017
- Messages
- 3
Hi everyone,
Long time reader, first time poster.
Sheet1 contains 196 rows that contain the word "ToReplace" as part of larger string.
Sheet2 contains 196 rows that contain only the word "ToReplace" in column A and a unique value in column B.
I need to replace Sheet1 "ToReplace" with the unique value in column B.
Sheet1 Sheet2
[TABLE="width: 500"]
<tbody>[TR]
[TD]ToReplace[/TD]
[TD]ToReplace[/TD]
[TD]12345[/TD]
[/TR]
[TR]
[TD]ToReplace[/TD]
[TD]ToReplace[/TD]
[TD]56784[/TD]
[/TR]
[TR]
[TD]ToReplace[/TD]
[TD]ToReplace[/TD]
[TD]76543[/TD]
[/TR]
</tbody>[/TABLE]
This is the code i found online:
Sub myReplace()
Dim myDataSheet As Worksheet
Dim myReplaceSheet As Worksheet
Dim myLastRow As Long
Dim myRow As Long
Dim myFind As String
Dim myReplace As String
' Specify name of Data sheet
Set myDataSheet = Sheets("Sheet1")
' Specify name of Sheet with list of replacements
Set myReplaceSheet = Sheets("Sheet2")
' Assuming list of replacement start in column A on row 2, find last entry in list
myLastRow = myReplaceSheet.Cells(Rows.Count, "A").End(xlUp).Row
Application.ScreenUpdating = False
' Loop through all list of replacments
For myRow = 2 To myLastRow
' Get find and replace values (from columns A and B)
myFind = myReplaceSheet.Cells(myRow, "A")
myReplace = myReplaceSheet.Cells(myRow, "B")
' Start at top of data sheet and do replacements
myDataSheet.Activate
Range("A1").Select
' Ignore errors that result from finding no matches
On Error Resume Next
' Do all replacements on column A of data sheet
Columns("A:A").Replace What:=myFind, Replacement:=myReplace, LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
' Reset error checking
On Error GoTo 0
Next myRow
Application.ScreenUpdating = True
MsgBox "Replacements complete!"
End Sub
Long time reader, first time poster.
Sheet1 contains 196 rows that contain the word "ToReplace" as part of larger string.
Sheet2 contains 196 rows that contain only the word "ToReplace" in column A and a unique value in column B.
I need to replace Sheet1 "ToReplace" with the unique value in column B.
Sheet1 Sheet2
[TABLE="width: 500"]
<tbody>[TR]
[TD]ToReplace[/TD]
[TD]ToReplace[/TD]
[TD]12345[/TD]
[/TR]
[TR]
[TD]ToReplace[/TD]
[TD]ToReplace[/TD]
[TD]56784[/TD]
[/TR]
[TR]
[TD]ToReplace[/TD]
[TD]ToReplace[/TD]
[TD]76543[/TD]
[/TR]
</tbody>[/TABLE]
This is the code i found online:
Sub myReplace()
Dim myDataSheet As Worksheet
Dim myReplaceSheet As Worksheet
Dim myLastRow As Long
Dim myRow As Long
Dim myFind As String
Dim myReplace As String
' Specify name of Data sheet
Set myDataSheet = Sheets("Sheet1")
' Specify name of Sheet with list of replacements
Set myReplaceSheet = Sheets("Sheet2")
' Assuming list of replacement start in column A on row 2, find last entry in list
myLastRow = myReplaceSheet.Cells(Rows.Count, "A").End(xlUp).Row
Application.ScreenUpdating = False
' Loop through all list of replacments
For myRow = 2 To myLastRow
' Get find and replace values (from columns A and B)
myFind = myReplaceSheet.Cells(myRow, "A")
myReplace = myReplaceSheet.Cells(myRow, "B")
' Start at top of data sheet and do replacements
myDataSheet.Activate
Range("A1").Select
' Ignore errors that result from finding no matches
On Error Resume Next
' Do all replacements on column A of data sheet
Columns("A:A").Replace What:=myFind, Replacement:=myReplace, LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
' Reset error checking
On Error GoTo 0
Next myRow
Application.ScreenUpdating = True
MsgBox "Replacements complete!"
End Sub