简体   繁体   English

VBA 文本框字体颜色

[英]VBA text box font color

I'm helping update Excel templates at my work and I need some help.我正在帮助更新我的工作中的 Excel 模板,我需要一些帮助。 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. EDIT1:如果我使用第 1 行和第 2 行或第 1 行和第 3 行,则以下代码有效。我不确定为什么不能同时使用两者。

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

@ LL The way you are doing it with shapes.addtextbox do it like this: @ LL 你用 shape.addtextbox 做的方式是这样的:

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

在此处输入图片说明

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 .下面的代码显示了如何使用 VBA 命名和处理文本框,添加文本等等。 . . 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?如果您有开发人员选项卡,最简单的方法是添加一个 ActiveX 文本框(我认为您将无法使用 .characters)但您可以指定文本框名称和许多属性:文本字体、大小、颜色( .forecolor) 在属性选项卡中或在 vba 中的文本框以及名称(容易使所有一种颜色,很难以这种方式仅将文本框中的选定文本更改为 VBA 中的某种颜色),但对于整个盒子? easy.简单。

If you do it this way and want to grab the contents of the textbox in vba, lets say you Dim aa comment string in your code and you name your activeX textbox TextBox1:如果您这样做并希望在 vba 中获取文本框的内容,假设您在代码中将注释字符串变暗,并将您的 activeX 文本框命名为 TextBox1:

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

在此处输入图片说明

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将此代码添加到上面的代码下方但就在 End Sub 之前,现在之前的代码是红色的,如果找到,您在代码块中定义的字符串将为蓝色,在这种情况下,我懒惰地将其分配给 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

在此处输入图片说明

I sincerely hope this helps you in your search of being a textBox jedi!我真诚地希望这可以帮助您成为一名 textBox 绝地! Cheers - WWC干杯 - WWC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM