Each character in a TextBox is a different color in vba excel

I am looking for a way to display each letter in a TextBox inside a UserForm in different colors using VBA. For example, the first character is red, the second is blue, the third .... Is there a way to do this?

+2
source share
1 answer

This is not possible with TextBox Control.
If you are using Excel 2010 or higher, you can use InkEdit Control.
This is an additional control under Tools> Advanced Controls (Excel VBE).

enter image description here

Once you have added it, it will be available in the toolbar as shown below.

enter image description here

UserForm.
, InkEdit Control, MSDN.
Btw, , ( Char) .

Private Sub InkEdit1_Change()

Dim i As Long

If Me.InkEdit1.Text <> "" Then
    For i = 1 To Len(Me.InkEdit1.Text)
        Me.InkEdit1.SelStart = i - 1 '~~> identifies the start of selection
        Me.InkEdit1.SelLength = 1 '~~> identifies the length of selection
        Select Case i
        Case 1: Me.InkEdit1.SelColor = RGB(255, 0, 0) '~~> colors 1st char Red
        Case 2: Me.InkEdit1.SelColor = RGB(0, 255, 0) '~~> colors 2nd char Green
        Case 3: Me.InkEdit1.SelColor = RGB(0, 0, 255) '~~> colors 3rd char Blue
        Case 4: Me.InkEdit1.SelColor = RGB(255, 255, 0) '~~> colors 4th char Yellow
        Case 5: Me.InkEdit1.SelColor = RGB(255, 0, 255) '~~> colors 5th char Indigo
        End Select
        Me.InkEdit1.SelStart = i '~~> this puts the cursor to end
    Next
End If

End Sub

, , 5.
InkEdit RGB , RGB.
, ; , .
-, Excel 2010, , .

:
enter image description here

+3

All Articles