我试图在VBA中设置图表点的DataLabel,但找不到解决方法。 在这段代码的最后一部分中,我设置了图表点的属性,但是我不知道如何设置自定义位置,我附上一张我想要得到的结果的图片:
objCht.Chart.SetSourceData Source:=ResBook.Sheets(Right(NombreHoja, 2)).Range("P1:R" & UltFilaGrafico)
objCht.Activate
Call AgregarEjeSecundario("Velocidad [km/h]", 3)
ActiveChart.Axes(xlValue, xlSecondary).Select
ActiveChart.Axes(xlValue, xlSecondary).MaximumScale = CInt(TargetVelocity) + 10
ActiveChart.Axes(xlValue, xlSecondary).MinimumScale = 0
ActiveChart.Legend.Position = xlLegendPositionBottom
'ActiveChart.SeriesCollection(2).Points(50).MarkerStyle = xlDiamond
With ActiveChart.SeriesCollection(1)
'.Points(StopEventRow).MarkerStyle = xlDiamond
.Points(StopEventRow).MarkerStyle = xlMarkerStyleTriangle
'.Points(StopEventRow).MarkerSize = 10
.Points(StopEventRow).MarkerBackgroundColor = RGB(255, 0, 0) 'Rojo
.Points(StopEventRow).MarkerForegroundColor = RGB(1, 1, 1)
.Points(StopEventRow).HasDataLabel = True
.Points(StopEventRow).DataLabel.text = "Detención"
'.Points(StopEventRow).ApplyDataLabels Type:=xlValue
.Points(StopEventRow).DataLabel.Font.ColorIndex = 3
'.Points(StopEventRow).DataLabel.Position = xlLabelPositionCustom
'.Points(StopEventRow).ApplyDataLabels Type:=xlShowLabel
End With
这就是我要通过VBA代码完成的工作。
在这里,我手动设置标签的位置,我正在尝试通过代码来完成此操作
答案 0 :(得分:1)
这是我使用宏记录器发现的一个基本示例:
Sub AddDataLabel()
Dim dl As DataLabel, cht As Chart, pt As Point
Set cht = ActiveSheet.ChartObjects(1).Chart
Set pt = cht.SeriesCollection(1).Points(6)
pt.ApplyDataLabels
Set dl = pt.DataLabel
With dl
.Text = "hello"
.Font.Color = vbRed
'move label relative to data point
.Top = pt.Top - 15
.Left = pt.Left + 15
End With
End Sub