【问题标题】:Extract Data Label from Excel Chart从 Excel 图表中提取数据标签
【发布时间】:2016-01-30 04:59:27
【问题描述】:

专家,

我正在尝试从自动生成的 Excel 图表中提取数据标签。我有大量的 VBA 经验,但不幸的是很少有图表。根据我对研究该主题的理解,有一种方法可以遍历图表上的所有项目以找到具有数据标签的项目。

输入: - 图表,例如“步骤 0.2” - 带有直线的散点图。

输出: - 从图表到单独工作表的数据标签 -位置,例如“主要”

一次尝试:

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

【问题讨论】:

    标签: vba excel charts


    【解决方案1】:

    这是从 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
    

    【讨论】:

    • 完美运行!非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-03
    • 1970-01-01
    • 2018-08-21
    • 2020-06-18
    • 1970-01-01
    • 2018-11-10
    相关资源
    最近更新 更多