Following up the above question and looking for further enhancements, is there a way to bold specific text ("price increase") and the characters in-between that text and special character ("~"), and then produce the expected results?
Expected results:
After price increase of ~0.1% vs others in Year 2020:
After ~6% - 7% price increase vs others in Year 2019:
Sub Colors()
Dim searchTerms As Variant
searchTerms = Array("price increase")
Dim searchString As String
Dim targetString As String
Dim offSet As Integer
Dim colToSearch As Integer
Dim arrayPos, rowNum As Integer
colToSearch = 6
For arrayPos = LBound(searchTerms) To UBound(searchTerms)
For rowNum = 8 To 15
searchString = Trim(searchTerms(arrayPos))
offSet = 1
Dim x As Integer
If (Not IsError(Cells(rowNum, colToSearch).Value)) Then
targetString = Cells(rowNum, colToSearch).Value
x = HilightString(offSet, searchString, rowNum, colToSearch)
End If
Next rowNum
Next arrayPos
End Sub
Function HilightString(offSet As Integer, searchString As String, rowNum As Integer, ingredCol As Integer) As Integer
Dim x As Integer
Dim newOffset As Integer
Dim targetString As String
' offSet starts at 1
targetString = Mid(Cells(rowNum, ingredCol), offSet)
foundPos = InStr(LCase(targetString), searchString)
If foundPos > 0 Then
' the found position will cause a highlight where it was found in the cell starting at the offset - 1
Cells(rowNum, ingredCol).Characters(offSet + foundPos - 1, Len(searchString)).Font.Bold= True
' increment the offset to found position + 1 + the length of the search string
newOffset = offSet + foundPos + Len(searchString)
x = HilightString(newOffset, searchString, rowNum, ingredCol)
Else
' if it's not found, come back out of the recursive call stack
Exit Function
End If
End Function
Refer to this: How to highlight selected text within excel