从Excel图表中提取数据标签

时间:2016-01-30 04:59:28

标签: excel vba excel-vba charts

专家,

我试图从自动生成的Excel图表中提取数据标签。我有相当数量的VBA经验,但遗憾的是图表很少。根据我对研究主题的理解,有一种方法可以遍历图表上的所有项目以查找具有数据标签的项目。

输入: -Chart Sheet,ex" Step 0.2" -Scatterplot有直线。

输出: - 从图表到单独工作表的数据标签 -Location,ex" Main"

尝试:

Option Explicit

'Loop through chart to find data labels
Sub FindDataLabels()

    'Define worksheet and chart
    Dim mainPage As Worksheet
    Dim TestChart As chart

    Set mainPage = ActiveWorkbook.Sheets("Main")
    Set TestChart = Charts("Step 0.2")

    'Extract data points from chart for analysis
    Dim line1 As SeriesCollection
    Set line1 = TestChart.SeriesCollection

    Dim dataPoints As Double
    dataPoints = line1.Count
Debug.Print "datapoints: "; dataPoints

    Dim LabelsArray(2) As Integer
    Dim i As Integer

    For i = 1 To dataPoints
        If line1(i).HasDataLabels Then

                Debug.Print "data label: "; line1(i).DataLabels.Text

        End If
    Next i

End Sub

1 个答案:

答案 0 :(得分:0)

以下是从Excel图表中提取数据标签值的代码

将值打印到立即窗口

Dim datapoint As Point

For Each datapoint In ActiveSheet.ChartObjects(1).Chart.SeriesCollection(1).Points

    'Extract data labels
    If datapoint.HasDataLabel Then Debug.Print datapoint.DataLabel.Text

Next