[英]How do I properly change customize the text inside of a textbox (shape)?
I have a macro that automatically inserts a textbox shape into a Chart and provides that textbox with the correct text and font.我有一个自动将文本框形状插入图表并为该文本框提供正确文本和字体的宏。 While it currently works as is, I want to better understand how to do this without using.Select on the textbox shape.
虽然它目前按原样工作,但我想更好地了解如何在不在文本框形状上使用 .Select 的情况下执行此操作。 The
.Select
method allows me to include .ColorIndex
, .Strikethrough
, .Superscript
, etc. but I am forced to put the .Textframe2.TextRange.Font.Fill.Forecolor.RGB
outside of the With Selection block to change the color. .Select
方法允许我包含.ColorIndex
、 .Strikethrough
、 .Superscript
等,但我不得不将.Textframe2.TextRange.Font.Fill.Forecolor.RGB
放在 With Selection 块之外以更改颜色。 (My current method to "update" the textbox deletes all existing textboxes in the chart and adds a new one). (我当前“更新”文本框的方法会删除图表中所有现有的文本框并添加一个新文本框)。
I've tried getting rid of .Select
and using "With txt1.Character(Start:=1, Length:=216).Font" and placing all contingencies within this With block, but errors occur with certain lines (I've read that it is good practice to avoid using.Select references).我已经尝试摆脱
.Select
并使用“With txt1.Character(Start:=1, Length:=216).Font”并将所有意外事件放入此 With 块中,但某些行会出现错误(我已阅读最好避免使用 .Select 引用)。
Really just looking to get a better understanding of how to properly and efficiently use a macro to update a textbox within a chart.真的只是想更好地了解如何正确有效地使用宏来更新图表中的文本框。
Sub UpdateChart()
Dim chrtobj As ChartObject
Dim txt1 As Shape
Dim cht1 As Chart
Dim wkbk As Workbook
Set wkbk = ActiveWorkbook
Set cht1 = Sheets("Profit & Loss").ChartObjects(1).Chart
On Error Resume Next
For Each chrtobj In Sheets("Profit & Loss").ChartObjects
chrtobj.Chart.TextBoxes.Delete
Next chrtobj
On Error GoTo 0
Set txt1 = cht1.Shapes.AddTextbox(msoTextOrientationHorizontal, 790, 30, 190, 30)
txt1.Line.Visible = msoFalse
txt1.TextFrame.Characters.Text = "Total Rev: $" & Format(Cells(n, "B").Value, "#,###")
txt1.Select
With Selection.Characters(Start:=1, Length:=216).Font
.Size = 18
.ColorIndex = xlAutomatic
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Bold = True
.Italic = True
End With
txt1.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(84, 130, 53)
End Sub
This works.这行得通。
I've removed the False
and xlAutomatic
values from the code as they're default values - no need to specifically set them.我已经从代码中删除了
False
和xlAutomatic
值,因为它们是默认值 - 无需专门设置它们。 Also the font colour is set when you use change the ForeColor
-当您使用更改
ForeColor
时,还会设置字体颜色 -
Also replaced the n
with 7
as n
wasn't defined in Format(Cells(n, "B").Value
还用
7
替换了n
,因为n
没有在Format(Cells(n, "B").Value
中定义
Sub UpdateChart()
Dim chrtobj As ChartObject
Dim txt1 As Shape
Dim cht1 As Chart
Dim wkbk As Workbook
Set wkbk = ActiveWorkbook
Set cht1 = Sheets("Profit & Loss").ChartObjects(1).Chart
On Error Resume Next
For Each chrtobj In Sheets("Profit & Loss").ChartObjects
chrtobj.Chart.TextBoxes.Delete
Next chrtobj
On Error GoTo 0
Set txt1 = cht1.Shapes.AddTextbox(msoTextOrientationHorizontal, 790, 30, 190, 30)
txt1.Line.Visible = msoFalse
txt1.TextFrame.Characters.Text = "Total Rev: $" & Format(Cells(7, "B").Value, "#,###")
'txt1.Select
With txt1.TextFrame2.TextRange.Characters.Font
.Size = 18
.Bold = True
.Italic = True
.Fill.ForeColor.RGB = RGB(84, 130, 53)
End With
'txt1.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(84, 130, 53)
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.