
Originally Posted by
Kenneth Hobs
UDF's have limited outputs. In the Test Sub, I put the result in D10.
For plain text, ConcatRangeChars() can be used as a UDF.
If needed a macro could check for the UDF formula and then run ConcatAndColorParts() for each. I would remove the ConcatRangeChars() in ConcatAndColorParts() if I did that. The problem then is that if the source data changed, no updates would be done.
[vba]Sub Test_ConcatAndColorParts()
Dim target As Range, source As Range
Set target = Range("D10")
Set source = Range("Test_Text")
ConcatAndColorParts target, source
End Sub
Sub ConcatAndColorParts(target As Range, source As Range, Optional delim As String = " ")
Dim cell As Range, v As Variant, i As Integer
target.Value2 = ConcatRangeChars(source, delim)
v = ""
i = 1
For Each cell In source
With cell
target.Characters(i, Len(.Value2)).Font.Color = .Font.Color
i = i + Len(.Value2) + Len(delim)
End With
Next cell
End Sub
Function ConcatRangeChars(charRange As Range, Optional delim As String = " ") As String
Dim v As String, cell As Range
Application.Volatile False
v = ""
For Each cell In charRange
v = v & cell.Text & delim
Next cell
v = Left(v, Len(v) - Len(delim))
ConcatRangeChars = v
End Function
[/vba]