简体   繁体   中英

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. 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. (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).

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. Also the font colour is set when you use change the ForeColor -

Also replaced the n with 7 as n wasn't defined in 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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