Sub ExtendColour()
Dim c As Range
Dim s As String
Dim pos1 As Long, pos2 As Long, i As Long, lens As Long
Application.ScreenUpdating = False
For Each c In Range("A1:C10")
With c
If Not IsNumeric(.Font.Color) Then
s = .Value & " "
lens = Len(s)
Do
pos1 = pos2 + 1
pos2 = InStr(pos1, s, " ")
For i = pos1 To pos2 - 1
If .Characters(i, 1).Font.Color > 0 Then
.Characters(pos1, pos2 - pos1).Font.Color = .Characters(i, 1).Font.Color
Exit For
End If
Next i
Loop Until pos2 = lens
End If
End With
Next c
Application.ScreenUpdating = True
End Sub
Hi Peter... are you back?It isn't clear what sheets, columns and rows to deal with, so see if this sets you in the right direction. It currently only checks A1:C10 on the active sheet.
If you have large ranges and/or cells that contain very long strings the code may take quite a while to run and there may also be unpredictable results.
Rich (BB code):Sub ExtendColour() Dim c As Range Dim s As String Dim pos1 As Long, pos2 As Long, i As Long, lens As Long Application.ScreenUpdating = False For Each c In Range("A1:C10") With c If Not IsNumeric(.Font.Color) Then s = .Value & " " lens = Len(s) Do pos1 = pos2 + 1 pos2 = InStr(pos1, s, " ") For i = pos1 To pos2 - 1 If .Characters(i, 1).Font.Color > 0 Then .Characters(pos1, pos2 - pos1).Font.Color = .Characters(i, 1).Font.Color Exit For End If Next i Loop Until pos2 = lens End If End With Next c Application.ScreenUpdating = True End Sub
The OP didn't mention "words" as such and asked to go forward/back until a space, not until space or punctuation, so I'm not sure that it is incorrect as posted. OP can clarify if required. (I did assume that the end of the string would act like a space though.)... words next to punctuation marks will color incorrectly with the code you posted ..
Okay, so you do not want periods, commas, question marks, etc. colored, correct?Yes until space [RHS or LHS] or punctuation, [RHS] found.
Please explain the above one... are you saying that if multiple words are enclosed in parentheses and only one of those words is partially colored, that all of the words within the parentheses plus the parentheses should get colored?Yes do not want periods, commas, question marks, etc. colored.
(two three)=(two three)