繁体   English   中英

Excel自动调整文字大小以适合形状

[英]Excel autosize text to fit shape

我想向多个图表模板添加一些VBA,以自动调整标题文本的大小以适合文本框。 现在可以运行,但是不会对文本框进行任何更改...

Sub TextBox()

Dim ws As Worksheet
Dim shp As Shape

' loop through sheets in this workbook
 For Each ws In ThisWorkbook.Worksheets
 ' loop through Chartobjects in sheet
   For Each shp In ws.Shapes

    If shp.Type = msoTextBox Then
        With shp.TextFrame2
            strTxt = .TextRange
            .DeleteText
            .WarpFormat = msoWarpFormat1
            .WordWrap = True
            .AutoSize = msoAutoSizeTextToFitShape
            .TextRange = strTxt
        End With
    End If
   Next shp
  Next ws

End Sub

您正在遍历工作表中的所有文本框,但是如果您的文本框位于chartobject上,则需要以略有不同的方式来处理它。

Sub TextBox()

    Dim w As Worksheet
    Dim c As ChartObject
    Dim s As Shape

    For Each w In ActiveWorkbook.Worksheets
        For Each c In w.ChartObjects
            For Each s In c.Chart.Shapes
                If s.Type = msoTextBox Then
                    With s.TextFrame2
                        strTxt = .TextRange
                        .DeleteText
                        .WarpFormat = msoWarpFormat1
                        .WordWrap = True
                        .AutoSize = msoAutoSizeTextToFitShape
                        .TextRange = strTxt
                        .TextRange.Font.Name = "Calibri"
                        .TextRange.Font.Size = "8"
                    End With
                End If
            Next s
        Next c
    Next w

End Sub

暂无
暂无

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

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