调整工作表中每个图表中所有数据标签的宽度

时间:2015-09-22 11:22:44

标签: excel excel-vba vba

我正在尝试从工作表的图表中获取代码来调整所有数据标签中的宽度,但我无法做到这一点。这里我有代码应用数字格式,我想要添加宽度属性(它只对Excel 2013有效):

Sub FormatAllCharts()
Dim ChtObj As ChartObject
For Each ChtObj In ActiveSheet.ChartObjects
With ChtObj.Chart
   For i = 1 To .SeriesCollection.Count
         With .SeriesCollection(i)
         .ApplyDataLabels
         .DataLabels.NumberFormat = "0,0;-0,0;;"
         End With
   Next
End With
Next
End Sub

这是用于更改数据标签宽度大小的代码:

ActiveChart.FullSeriesCollection(1).DataLabels.Select
ActiveChart.FullSeriesCollection(1).Points(4).DataLabel.Select
Selection.Width = 19

1 个答案:

答案 0 :(得分:0)

在这里,我最终找到了解决方案:

Sub FormatAllCharts()
Dim i As Long
Dim oChtObj As ChartObject

    For Each oChtObj In ActiveSheet.ChartObjects
        With oChtObj.Chart
            For i = 1 To .SeriesCollection.Count
                With .SeriesCollection(i)
                  .ApplyDataLabels
                  .DataLabels.NumberFormat = "0,0;-0,0;;"
                    Values_Array = .Values
                  For j = LBound(Values_Array, 1) To UBound(Values_Array, 1)
                     .Points(j).DataLabel.Width = 19
                  Next
                End With
            Next
        End With
    Next
End Sub