简体   繁体   English

如何正确更改自定义文本框(形状)内的文本?

[英]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.我已经从代码中删除了FalsexlAutomatic值,因为它们是默认值 - 无需专门设置它们。 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.

相关问题 VBA - 如何将换行命令 (\n) 或制表符命令 (\t) 发送到 PowerPointS 形状的 textbox.textrange.text - VBA - How do I send new line command (\n) or tab command (\t) to a textbox.textrange.text of a PowerPointS Shape 如何确定是否单击了userform1内的文本框? 并获取其值,以便我可以做msgbox然后显示文本? - How to determine if textbox inside userform1 was clicked? and get its value so I can do msgbox then display the text? 如何通过单击另一个形状来更改形状的形状大小? - How can I change the shape size of a shape by clicking another shape? 如何在C#Excel Interop中将文本添加到现有形状(文本框) - How to add text to an existing shape(textbox) in C# Excel Interop Excel VBA:如何更改文本框中的字体大小和对齐方式 - Excel VBA: How do I change font size and alignment inside text boxes 如何在不选择形状的情况下使用VBA更改Excel中的文本大小? - How can I change the text size in Excel using VBA without selecting the shape? 如何使用Rails自定义电子表格 - How do i customize spreadsheet with ruby on rails 如何自定义电子表格gem /输出? - How do I customize the spreadsheet gem/output? 如何在形状的节点周围移动? - How do I move around nodes in a shape? 如何在不更改公式的情况下更改单元格的文本? - How do I change the text of a cell without changing the formula?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM