How to change a string color using vba

Copper Contributor


I'm printing a code using some vba functions and I need that the color of the last 6 digits of that code (UDI string) be red, colud some one help me please?


result of the genUDI fuction - (21)53201.0001.009B4CS01

result that I want - (21)53201.0001.009B4CS01

I'm using this vba fuctions to generate the code:


Function fBase34(ByRef lngNumToConvert As Long) As String
'Converte base 10 para 34 (base 36 sem I e O)
Dim strAlphabet As String

strAlphabet = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ"

If lngNumToConvert = 0 Then
Base34Encode = "0"
Exit Function
End If

fBase36Encode = vbNullString

Do While lngNumToConvert <> 0
fBase34 = Mid(strAlphabet, lngNumToConvert Mod 34 + 1, 1) & fBase34
lngNumToConvert = lngNumToConvert \ 34

If Len(fBase34) = 1 Then
fBase34 = "0" + fBase34
End If

End Function

Function genUDI(ByRef decNum As Long, prefixo As String) As String
'Gera UDI com base em um numero decimal
Dim UDI As String
UDI = Right(prefixo, 4) & fBase34(decNum)

genUDI = prefixo & UDI

End Function

1 Reply


It is not possible to color part of the result of a formula. Excel simply doesn't work that way.

You'd have to replace the result of the formula with its value, then  color the last six digits.

But the cell wouldn't be updated anymore if the cell(s) that the formula referred to change...