宏中的标签

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了宏中的标签相关的知识,希望对你有一定的参考价值。

我正在运行一个宏,我曾用它来创建多个线图,这些线图在下一个工作表中作为输出创建。我需要为折线图添加自定义数据标签。我似乎无法获得自定义标签的vba来选择(Cells(i, 10), Cells(i, 21))作为自定义标签。

答案

要将第5行的名称设置为“Downtown”,您的代码将类似于:

chrt.SeriesCollection(5).Name = "Downtown"

要确保您的数据点有标签,您可以检查和/或设置

chrt.SeriesCollection(5).HasDataLabels = True

要将第3个点的标签更改为“北”

chrt.SeriesCollection(5).DataLabels(3).Text = "North"

希望有所帮助。

另一答案

这样的事情可能就是你想要的:

Sub AddCharts()

    'variable declaration
    Dim sheetSource As String
    sheetSource = "sheet5"      'update with sheet name

    Dim sheetDestination As String
    sheetDestination = "sheet6" 'update with sheet name

    With Sheets(sheetSource)
        'Find the last used row
        Dim lastRow As Long
        lastRow = .Cells(.Rows.count, "A").End(xlUp).row
    End With

    Dim i As Long
    Dim j As Long
    Dim chrt As Chart
    Dim rangeSource As Range
    Dim rangeLabels As Range
    Dim cel As Range

    'Destination sheet is selected bcoz charts will be inserted here
    Sheets(sheetDestination).Select

    'Looping from second row till last row with data
    For i = 2 To lastRow

        With Sheets(sheetSource)
            'set the chart source
            Set rangeSource = .Range(.Cells(i, "D"), .Cells(i, "O"))  'your sheet confused me on the end column

            'pick the range of labels
            Set rangeLabels = .Range(.Cells(i, "AM"), .Cells(i, "AX"))
        End With

        'Adds chart to destination sheet
        Set chrt = Sheets(sheetDestination).Shapes.AddChart.Chart

        With chrt

            'set the chart as a line chart
            .ChartType = xlLine

            'set chart source
            .SetSourceData Source:=rangeSource

            With .SeriesCollection(1)
                .name = Sheets(sheetSource).Range("Q1").Value2
                .XValues = "{""July"",""Aug"",""Sep"",""Oct"",""Nov"",""Dec"",""Jan"",""Feb"",""Mar"",""Apr"",""May"",""June""}"
                .ChartType = xlLineMarkers
            End With

            'add another series of data
            .SeriesCollection.Add Source:=Sheets(sheetSource).Range(Sheets(sheetSource).Cells(i, "P"), Sheets(sheetSource).Cells(i, "AA"))

            .SeriesCollection(2).name = Sheets(sheetSource).Range("P1").Value2

            'turns labels on
            .SeriesCollection(2).ApplyDataLabels

            j = 1
            For Each cel In rangeLabels
                .SeriesCollection(2).DataLabels(j).Text = cel.Value2
                j = j + 1
            Next cel

            .HasTitle = True

            'move the chart to left and below previous charts
            With .ChartArea
                .Left = 1
                .Top = (i - 2) * .HEIGHT
            End With

        End With

    Next

End Sub

与您的更改最相关的位是:

j = 1
For Each cel In rangeLabels
    .SeriesCollection(1).DataLabels(j).Text = cel.Value2
    j = j + 1
Next cel

因为它循环遍历带有标签文本的范围,(.Cells(i, "AM"), .Cells(i, "AX"))并按顺序将这些单元格值作为标签应用于数据点。我没有做太多的错误处理,所以如果标签多于数据点,它可能会破坏你。

以上是关于宏中的标签的主要内容,如果未能解决你的问题,请参考以下文章

为啥用三元运算符来定义宏中的 1 和 0?

Excel VBA 宏中的数组

在 C 中的宏中替换宏

宏中的波浪号 (~) 是啥意思?

Scheme宏中的任意计算

imageJ 宏中的字符串格式