Hi there,
Here is my task:
Sheet2: Column A will contain string that i want to find in Sheet1 and replace with the values given in Columns B to myLastColumn.
Sheet1: Column A will contain identical string from row 1 to myLastRow. Each cell will contain string found in Sheet2, column A. I want to replace this string with values from Sheet2, Columns B to myLastColumn.
However;
Sheet1, Cell A1 will be replaced by values in Sheet2, column B (10 rows).
Then, Sheet1, Cell A2 will be replaced by Sheet2, column C (10 rows).
Then, Sheet1, Cell A3 will be replaced by Sheet2, Column D (10 rows).
This will continue until the lastrow is reached in Sheet1 and the last column is reach in Sheet2.
Thanks in advance!!
Here is my task:
Sheet2: Column A will contain string that i want to find in Sheet1 and replace with the values given in Columns B to myLastColumn.
Sheet1: Column A will contain identical string from row 1 to myLastRow. Each cell will contain string found in Sheet2, column A. I want to replace this string with values from Sheet2, Columns B to myLastColumn.
However;
Sheet1, Cell A1 will be replaced by values in Sheet2, column B (10 rows).
Then, Sheet1, Cell A2 will be replaced by Sheet2, column C (10 rows).
Then, Sheet1, Cell A3 will be replaced by Sheet2, Column D (10 rows).
This will continue until the lastrow is reached in Sheet1 and the last column is reach in Sheet2.
Code:
Sub myReplace()
Dim myDataSheet As Worksheet
Dim myReplaceSheet As Worksheet
Dim myLastRow As Long
Dim myRow As Long
Dim myColumn As Long
Dim myLastColumn 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 to search start in column A on row 2, find last entry in list
myLastRow = myReplaceSheet.Cells(Rows.Count, "A").End(xlUp).Row
' Assuming list of replacement start in column B on row 2, find last entry in list
myLastColumn = myReplaceSheet.Cells(2, Columns.Count).End(xlToLeft).Column
Application.ScreenUpdating = False
' Loop through all list of replacments
For myRow = 2 To myLastRow
For myColumn = 2 To myLastColumn
' Get find and replace values (from columns A and B)
myFind = myReplaceSheet.Cells(myRow, "A")
myReplace = myReplaceSheet.Cells(myRow, myColumn)
' 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 myColumn
Next myRow
Application.ScreenUpdating = True
MsgBox "Replacements complete!"
End Sub
Thanks in advance!!