Find a set of characters embedded in a word and change the entire words format

KYLE05

New Member
Joined
Aug 26, 2014
Messages
4
I am new to VBA and have not been able to find a solution to a current problem I am having. The title may be confusing but this is what I want to do.

I want to search the entire document for these sets of characters
"ZJIGS" and "ZMOLD".

Then change their color without changing the color of other words in the cell which I have been able to do. The issue is they are always found imbedded in a word in this format
"ZJIGS-XXXX###-####-#" or "ZMOLD-XXXX###-####-#"

I want to change the entire string of characters colors shown above. My previous efforts have only been able to change the color of the ZJIGS or ZMOLD portion.
I am using excell 2010

Thank you
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Code:
Sub colorWords()

For Each Cell In ActiveWorkbook.ActiveSheet.UsedRange
    If InStr(1, Cell.value, "ZMOLD") >= 0 Or InStr(1, Cell.value, "ZJIGS") > 0 Then
        Cell.Font.color = vbBlue
    End If
    
Next
End Sub
 
Upvote 0
Does this macro do what you want...
Code:
Sub ZJIGSandZMOLD()
  Dim ZJIG As Long, ZMOLD As Long, Cell As Range
  Application.ScreenUpdating = False
  For Each Cell In ActiveSheet.UsedRange
    ZJIG = InStr(1, Cell.Value, "ZJIG", vbTextCompare)
    ZMOLD = InStr(1, Cell.Value, "ZMOLD", vbTextCompare)
    Do While ZJIG + ZMOLD > 0
      If ZJIG Then
        Cell.Characters(ZJIG, 21).Font.ColorIndex = 3
        ZJIG = InStr(ZJIG + 1, Cell.Value, "ZJIG", vbTextCompare)
      ElseIf ZMOLD Then
        Cell.Characters(ZMOLD, 21).Font.ColorIndex = 3
        ZMOLD = InStr(ZMOLD + 1, Cell.Value, "ZMOLD", vbTextCompare)
      End If
    Loop
  Next
  Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,880
Messages
6,181,532
Members
453,054
Latest member
ezzat

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top