jgarner123
New Member
- Joined
- May 2, 2014
- Messages
- 9
I have a list of words that I need to bold and change the color. For example,
gross proceeds needs to be bold and red
market value needs to be bold and red
enhancing the value needs to be bold and blue
used off needs to be bold and green
I only need exact matches. Also these words can appear multiple times in the same cell. I have used a code that only changes the words the first time they are in the cell - the words might be in the same cell 5 times but only the first instance is fixed. Also this code is only for one specific example so I have to change it and run it several times. I would like to just run it once. In addition to the above 4 examples I have about 30 other combinations.
The code I have used is below:
Sub colorText()
Dim cl As Range
Dim startPos As Integer
Dim totalLen As Integer
Dim searchText As String
' specify text to search. (Change to your needs)
' searchText = [A1]
' searchText = Application.InputBox("enter search text")
searchText = "market value"
' loop trough all cells in selection/range (Change to your needs)
'For Each cl In Range("b:b")
'For Each cl In Selection
'For Each cl In Range("b1", Range("b65536").End(xlUp))
For Each cl In Range("b1:b222")
totalLen = Len(searchText)
startPos = InStr(cl, searchText)
If startPos > 0 Then
With cl.Characters(startPos, totalLen).Font
.FontStyle = "Bold"
.ColorIndex = 10
End With
End If
Next cl
End Sub
gross proceeds needs to be bold and red
market value needs to be bold and red
enhancing the value needs to be bold and blue
used off needs to be bold and green
I only need exact matches. Also these words can appear multiple times in the same cell. I have used a code that only changes the words the first time they are in the cell - the words might be in the same cell 5 times but only the first instance is fixed. Also this code is only for one specific example so I have to change it and run it several times. I would like to just run it once. In addition to the above 4 examples I have about 30 other combinations.
The code I have used is below:
Sub colorText()
Dim cl As Range
Dim startPos As Integer
Dim totalLen As Integer
Dim searchText As String
' specify text to search. (Change to your needs)
' searchText = [A1]
' searchText = Application.InputBox("enter search text")
searchText = "market value"
' loop trough all cells in selection/range (Change to your needs)
'For Each cl In Range("b:b")
'For Each cl In Selection
'For Each cl In Range("b1", Range("b65536").End(xlUp))
For Each cl In Range("b1:b222")
totalLen = Len(searchText)
startPos = InStr(cl, searchText)
If startPos > 0 Then
With cl.Characters(startPos, totalLen).Font
.FontStyle = "Bold"
.ColorIndex = 10
End With
End If
Next cl
End Sub