I have a workbook with 2 sheets. Sheet 1 cell A1 has a black text sting in it. Sheet 2 has two columns I'm working with, column A (the find column) and column B (the replace column). Sheet 2 columns A (the find column) & B (the replace column) have text strings in them. The text stings in Sheet 2 columns A (the find column) and B (the replace column) are also black.
I'm trying to search the text string in Sheet 1 cell A1, see if it contains the text string from Sheet 2 cell A2 (the find column), and, if it does, replace that part of the text string in Sheet 1 cell A1 with (a red text version of) the text string in Sheet 2 cell B1 (the replace column).
I would like the macro to loop through all the used rows in Sheet 2 column A, if Sheet 1 cell A1 contains text string from the remaining used rows in Sheet 2 column A, again replacing that part of the text string in Sheet 1 cell A1 with (a red text version of) the text string in Sheet 2 cell B1 (the replace column).
There's a better way of saying that. But to be clear, I don't want to replace the entire contents of Sheet 1 cell A1, just (a red text version of) the text string from Sheet 2 cell B1.
The find replace part works great. But I can't seem to get the replaced parts of the text string in Sheet 1 cell A1 to turn red and stay red.
Any help would be greatly appreciated!
Here's the code I'm working with so far:
Sub FindReplace()
Dim mySheet 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 sheet
Set mySheet = Sheets("Strings")
' Specify name of Sheet with list of finds
' Specify name of Sheet with list of finds and replacements
Set myReplaceSheet = Sheets("Synonyms")
' Assuming the list of that need replaced start in column B on row 1, 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 = 1 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
mySheet.Activate
Range("B1").Select
' Ignore errors that result from finding no matches
On Error Resume Next
' Do all replacements on column A of data sheet
ColorReplacement Sheets("Strings").Range("A1"), myFind, myReplace
' Reset error checking
On Error GoTo 0
Next myRow
Application.ScreenUpdating = True
End Sub
Sub ColorReplacement(aCell As Range, findText As String, ReplaceText As String, Optional ReplaceColor As OLE_COLOR = vbRed)
Dim oText As String, nText As String, counter As Integer
oText = aCell.Cells(1, 1).Text
nText = Replace(oText, findText, ReplaceText, 1, 1000000)
If oText <> nText Then
aCell.Cells(1, 1).Value = nText
For counter = 0 To Len(aCell.Cells(1, 1))
If aCell.Characters(counter, Len(ReplaceText)).Text = ReplaceText Then
aCell.Characters(counter, Len(findText) + 1).Font.Color = ReplaceColor
End If
Next
End If
End Sub