Home » excel » excel – VBA text box font color

excel – VBA text box font color

Posted by: admin April 23, 2020 Leave a comment

Questions:

I’m helping update Excel templates at my work and I need some help.
I’m trying to format text in a textbox to be red and remove the border. I’m not sure how to add that property to my code. I’m not very good at coding. I was able to put the below together from dissecting other code I found.

ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 2525, 1800, 103, 24).TextFrame.Characters.Text = Format(Cells(9, 2), "mmmm d, yyyy")

I’ll eventually replace the absolute location values in the textbox code but I want it to work before I start making efficiency tweaks. Thanks for helping!

EDIT1: The below code works if I use either line 1 and 2 or Line 1 and 3. I’m not sure why I cannot use both in conjuction.

ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 2525, 1800, 103, 24) _
.TextFrame.Characters.Font.Color = vbRed _
.TextFrame.Characters.Text = Format(Cells(9, 2), "mmmm d, yyyy")
How to&Answers:

@ L L
The way you are doing it with shapes.addtextbox do it like this:

With ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 146#, 68#, _
        301#, 181#).TextFrame
        .Characters.Font.Color = RGB(255, 0, 0)
        .Characters.Text = "Whatever your string is"
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
 End With

enter image description here

If I keep typing in the text box then the rest is wrapped in red as well as whatever was in there with the first assignment, the command button in the screenshot executes the code above just to play around.

If you are going to keep adding and subtracting text to the textbox with code it is better if you get the shape named. Then you can grab the string that is in there and add to it . . This code below shows how you can name and then play around with your text box using VBA, adding text and so forth . . you can now change the color of certain text within the textbox as well, which is cool but another topic maybe?

Option Explicit

Private Sub CommandButton1_Click()

Dim tbShape As Shape
Dim WKS As Worksheet
Dim newText As String
Dim previousText As String
Dim textRange As Range

Set WKS = ThisWorkbook.ActiveSheet
Set textRange = WKS.Range("C1") 'define where to grab the new text
newText = CStr(textRange) 'convert what is in the cell to string

With ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 146#, 68#, _
            301#, 181#)
            .Name = "myTextBox"
End With

Set tbShape = ActiveSheet.Shapes("myTextBox")

With tbShape.TextFrame
    .Characters.Font.Color = RGB(255, 0, 0)
    .Characters.Text = "This is the intial text of the textbox 11111111 222222 "
    .HorizontalAlignment = xlCenter
    .VerticalAlignment = xlCenter
End With

'grab the previous textbox string
previousText = tbShape.TextFrame.Characters.Text
'add your new text to what was there before
tbShape.TextFrame.Characters.Text = previousText & newText

End Sub

So I hope this helps. The easiest way out if you have the developer tab is to add an ActiveX textbox (you will not be able to use .characters I don’t think) but you can assign the textbox name and many properties: text font, size, color (.forecolor) of that text box right in the properties tab or in vba as well with the name (easy to make all one color, hard to change only selected text in the textbox to a certain color in VBA this way), but for the whole box? easy.

If you do it this way and want to grab the contents of the textbox in vba, lets say you Dim a a comment string in your code and you name your activeX textbox TextBox1:

'get comments
comments = msrSheet.OLEObjects("TextBox1").Object.Text

enter image description here

you have to use the first way of inserting a textbox (the way you are currently using and the first way in the answer) to change only selected text to a new color as far as I have found. Want to see it in action? Add this code below what is above but just before End Sub, now what was there previously is red, the string you define in the code block will be blue if found, in this case I lazily assign it to newText

'grab the previous textbox string
previousText = tbShape.TextFrame.Characters.Text
'Change the color of the new string in the textbox to blue
Dim blueText As String
blueText = newText
With tbShape.TextFrame
    .Characters(InStr(previousText, blueText), Len(blueText)).Font.Color = RGB(0, 0, 255)
End With

enter image description here

I sincerely hope this helps you in your search of being a textBox jedi!
Cheers – WWC