【问题标题】:Set Data Labels as values from Range in VBA在 VBA 中将数据标签设置为 Range 中的值
【发布时间】:2018-08-28 15:30:54
【问题描述】:

我正在尝试将我的数据标签设置为等于自定义值。但是,它们没有出现。我已经明确定义了它们可能出现的范围,不知道为什么没有。

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

我可以将名称作为数组传递吗?

【问题讨论】:

  • 以下是使用公式而不是 vba peltiertech.com/apply-custom-data-labels-to-charted-points 的方法
  • Kinda 需要是 VBA。
  • 您的意思是让这一行成为names.Cells(k, 1).Text 以匹配您增加的变量吗? k = k + 1
  • 我在代码中更改了它,但没有任何更新。
  • 如果为了测试而将其替换为pt.DataLabel.Text = "hi",它是否会更改标签

标签: arrays vba charts format


【解决方案1】:

我发现您的代码存在 2 个问题。您仅将标签格式应用于最后一点。其次你没有写.HasDataLabel=True。试试下面的代码(注意我假设你已经正确定义了mysrs

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

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多