【问题标题】:Datalabel for XY chart in Excel VBAExcel VBA中XY图表的数据标签
【发布时间】:2021-11-03 07:57:02
【问题描述】:

我需要修改 vba 上的代码,以便在 XY 图上进行标记。我有一个代码,当样本系列在水平位置共享时效果很好。但我有一个系列,实际上是由两个不同的系列组成,一个接一个地摆在桌子上。让我把它显示在图片上,这也显示了我的目标。

我使用的当前代码不适用于我的实际情况:

 'Name macro
Sub AddDataLabels()
 
'Enable error handling
On Error Resume Next
 
'Display an inputbox and ask the user for a cell range
Set Rng = Application.InputBox(prompt:="Select cells to link" _
, Title:="Select data label values", Default:=ActiveCell.Address, Type:=8)
 
'Disable error handling
On Error GoTo 0
 

With ActiveChart
 
'Iterate through each series in chart
For Each ChSer In .SeriesCollection

    'Save chart point to object SerPo
    Set SerPo = ChSer.Points

    'Save the number of points in chart to variable j
    j = SerPo.Count

    'Iterate though each point in current series
    For i = 1 To j

        'Enable data label for current chart point
        SerPo(i).ApplyDataLabels Type:=xlShowValue

        'Save cell reference to chart point
        SerPo(i).DataLabel.FormulaLocal = Rng.Cells(i).FormulaLocal
        '& rng.Cells(i).Reference(ReferenceStyle:=xlR1C1)
    Next
Next
End With
End Sub

如果我为上述数据系列运行此代码,图表将如下所示:

那么如何修改此代码以获得目标图表,请帮助我。

【问题讨论】:

    标签: excel vba labeling


    【解决方案1】:

    问题在于,在 For 循环中,“i”变量只转到 j,在本例中为 3。您需要一个变量来控制标签,使其从 1 变为 6(标签的选定范围)。

    我创建了一个变量 curLabel,它在 for 循环中使用并递增。这是你要找的吗?

    Sub AddDataLabels()
        Dim curLabel As Integer: curLabel = 1
        
        'Enable error handling
        On Error Resume Next
         
        'Display an inputbox and ask the user for a cell range
        Set Rng = Application.InputBox(prompt:="Select cells to link" _
            , Title:="Select data label values", Default:=ActiveCell.Address, Type:=8)
            
        'Disable error handling
        On Error GoTo 0
    
        With ActiveChart
                
            'Iterate through each series in chart
            For Each ChSer In .SeriesCollection
    
                'Save chart point to object SerPo
                Set SerPo = ChSer.Points
    
                'Save the number of points in chart to variable j
                j = SerPo.Count
    
                'Iterate though each point in current series
                For i = 1 To j
    
                    'Enable data label for current chart point
                    SerPo(i).ApplyDataLabels Type:=xlShowValue
    
                    'Save cell reference to chart point
                    SerPo(i).DataLabel.FormulaLocal = Rng.Cells(curLabel).FormulaLocal
                    '& rng.Cells(i).Reference(ReferenceStyle:=xlR1C1)
                    
                    ' Next label
                    curLabel = curLabel + 1
                Next
            Next
        End With
    End Sub
    

    您可以更改单元格值“水平系列”和“垂直系列”,并且图例会自动更新,或者您可以在其他地方设置图例,如下面的代码。

    Sub AddCustomLegend()
        Dim myLegend As String
        
        ' series 1 legend
        myLegend = "=" & ActiveSheet.Name & "!" & Range("C12").Address
        ActiveChart.SeriesCollection(1).Name = myLegend
    
        ' series 2 legend
        myLegend = "=" & ActiveSheet.Name & "!" & Range("D12").Address
        ActiveChart.SeriesCollection(2).Name = myLegend
    End Sub
    

    【讨论】:

    • 我很欣赏你,你很棒。非常感谢。
    • 这个解决方案有一个副作用,我们可以操纵图例名称吗?因为我想给他们不同的名称,这些名称是从单元格值分配的。你会给我什么建议?
    • 现在我正试图让我的标签从非连续单元格中写入。我集成了下面的代码块,但无论如何都不起作用:使用 ActiveChart If Rng.Areas.Count > 1 Then 'Debug.Print "It's a non-contiguous range" For Each rngArea In Rng.Areas rwCount = rwCount + rngArea.Rows. Count Next End If -> 我使用这个块来获取联合单元格的面积。然后在此行中使用计数:SerPo(i).DataLabel.FormulaLocal = Rng.Areas(rwCount).Cells(curLabel).FormulaLocal。其余代码与您更正的代码相同。
    • 这有点令人困惑,所以如果您不介意编辑您的帖子并将所有更改粘贴到新的“AddDataLabels”过程中。同时发布非连续细胞的图像。
    • 请点击这个链接,我在不同的问题上提出了我的问题。 stackoverflow.com/questions/69075951/…
    猜你喜欢
    • 2021-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多