简体   繁体   中英

Set Data Labels as values from Range in VBA

I am trying to set my Data Labels equal to custom values. However, they do not appear. I have clearly defined the range in which they might appear and do not know why they don't.

Dim names As Range
Set names = Range(Range("A2"), Range("A2").End(xlDown))

Set mypts = mysrs.Points

mypts(mypts.count).ApplyDataLabels

With mypts(mypts.count).DataLabel
    .ShowSeriesName = False
    .ShowCategoryName = False
    .ShowValue = False
    ' optional parameters
    .Position = xlLabelPositionAbove
    .Font.name = "Helvetica"
    .Font.Size = 10
    .Font.Bold = False
End With

For Each pt In mypts
    k = k + 1
    pt.DataLabel.Text = names.Cells(k, 1).Text
Next

Could I pass through the names as an array?

I found 2 issues with your code. You are applying the label formating only to the last point. Second you did not write .HasDataLabel=True .Try the following code (Note I am assuming you already defined mysrs correctly)

Dim names As Range
Set names = Range(Range("A2"), Range("A2").End(xlDown)

Set mypts = mysrs.Points

'mypts(mypts.Count).ApplyDataLabels

For k = 1 To mypts.Count

    mypts(k).HasDataLabel = True
    mypts(k).DataLabel.Text = names.Cells(k, 1).Text

    With mypts(k).DataLabel
    .ShowSeriesName = False
    .ShowCategoryName = False
    .ShowValue = False
    ' optional parameters
    .Position = xlLabelPositionAbove
    .Font.Name = "Helvetica"
    .Font.Size = 10
    .Font.Bold = False

    End With

Next k

Hope This helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM