2017-11-15 1 views
0

Ich möchte einige VBA zu einer Vorlage für mehrere Diagramme hinzufügen, um den Text automatisch an die Textbox anzupassen. Dies läuft jetzt, aber macht keine Änderungen an Textfeld ...Excel Größe automatisch anpassen, um Form zu passen

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 
+0

Sie haben nicht gesetzt TB etwas zu sein, bevor Sie versuchen, es zu benutzen –

+0

versucht haben, dieses Set TB = Shapes.Range (Array ("TextBox 1")) – tj123

+0

@ tj123 welche Art von Textfeld verwenden Sie in deiner Datei? –

Antwort

0

Sie werden durch die ganze Textbox in den Blättern Looping, aber wenn Ihr Textbox auf ChartObject- ist, müssen Sie es in einem leicht zu handhaben anders.

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 
+0

Dies funktioniert, dass es den Text massiv schrumpft, aber gibt es sowieso calibri 18 Schriftart als Standard hinzufügen? – tj123

+0

@ tj123 Versuchen Sie es jetzt. –

Verwandte Themen