VBA 将数据点格式化为图表中数据的最后一个点的问题
Issues with VBA for data point formatting to the last point with data in a chart
我有这段代码可以将数据标签应用于图表中数据的最后一点。我已经添加了额外的代码(在它下面单独添加),它为最后一点添加了额外的格式。这种额外的格式似乎没有得到应用,我没有收到任何错误。
Sub LastPointLabel2()
Dim srs As Series
Dim iPts As Long
Dim cht As ChartObject
Dim vYVals As Variant
Dim vXVals As Variant
Set ws = ActiveSheet
If ActiveChart Is Nothing Then
MsgBox "Select a chart and try again.", vbExclamation
Else
Application.ScreenUpdating = False
For Each cht In ws.ChartObjects
Set srs = cht.Chart.SeriesCollection(1)
With srs
vYVals = .Values
'vXVals = .XValues
' clear existing labels
.HasDataLabels = False
For iPts = .Points.Count To 1 Step -1
If Not IsEmpty(vYVals(iPts)) Then
' add label
srs.Points(iPts).ApplyDataLabels _
ShowSeriesName:=False, _
ShowCategoryName:=False, ShowValue:=True, _
AutoText:=True, LegendKey:=False
Exit For
End If
'Reposition label
If srs.Points(iPts).HasDataLabel Then
With srs.Points(iPts).DataLabel
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlTop
.ReadingOrder = xlLTR
.Position = xlLabelPositionAbove
.Orientation = xlHorizontal
End With
End If
'Adjust label size and colour
If srs.Points(iPts).HasDataLabel Then
With srs.Points(iPts)
.MarkerSize = 7
.MarkerStyle = xlCircle
.MarkerBackgroundColorIndex = 6
.MarkerForegroundColorIndex = 1
End With
End If
Next
End With
Next
' legend is now unnecessary
Application.ScreenUpdating = True
End If
End Sub
这部分似乎没有得到应用
'Reposition label
If srs.Points(iPts).HasDataLabel Then
With srs.Points(iPts).DataLabel
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlTop
.ReadingOrder = xlLTR
.Position = xlLabelPositionAbove
.Orientation = xlHorizontal
End With
End If
'Adjust label size and colour
If srs.Points(iPts).HasDataLabel Then
With srs.Points(iPts)
.MarkerSize = 7
.MarkerStyle = xlCircle
.MarkerBackgroundColorIndex = 6
.MarkerForegroundColorIndex = 1
End With
End If
如果我删除 Exit For
,则上述格式将应用于所有数据点
Sub LastPointLabel2()
Dim srs As Series
Dim iPts As Long
Dim cht As ChartObject
Dim vYVals As Variant
Dim vXVals As Variant
Set ws = ActiveSheet
If ActiveChart Is Nothing Then
MsgBox "Select a chart and try again.", vbExclamation
Else
Application.ScreenUpdating = False
For Each cht In ws.ChartObjects
Set srs = cht.Chart.SeriesCollection(1)
With srs
vYVals = .Values
'vXVals = .XValues
' clear existing labels
.HasDataLabels = False
For iPts = .Points.Count To 1 Step -1
If Not IsEmpty(vYVals(iPts)) Then
' add label
srs.Points(iPts).ApplyDataLabels _
ShowSeriesName:=False, _
ShowCategoryName:=False, ShowValue:=True, _
AutoText:=True, LegendKey:=False
'Reposition label
If srs.Points(iPts).HasDataLabel Then
With srs.Points(iPts).DataLabel
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlTop
.ReadingOrder = xlLTR
.Position = xlLabelPositionAbove
.Orientation = xlHorizontal
End With
End If
'Adjust label size and colour
If srs.Points(iPts).HasDataLabel Then
With srs.Points(iPts)
.MarkerSize = 7
.MarkerStyle = xlCircle
.MarkerBackgroundColorIndex = 6
.MarkerForegroundColorIndex = 1
End With
End If
Exit For
End If
Next
End With
Next
' legend is now unnecessary
Application.ScreenUpdating = True
End If
End Sub
我有这段代码可以将数据标签应用于图表中数据的最后一点。我已经添加了额外的代码(在它下面单独添加),它为最后一点添加了额外的格式。这种额外的格式似乎没有得到应用,我没有收到任何错误。
Sub LastPointLabel2()
Dim srs As Series
Dim iPts As Long
Dim cht As ChartObject
Dim vYVals As Variant
Dim vXVals As Variant
Set ws = ActiveSheet
If ActiveChart Is Nothing Then
MsgBox "Select a chart and try again.", vbExclamation
Else
Application.ScreenUpdating = False
For Each cht In ws.ChartObjects
Set srs = cht.Chart.SeriesCollection(1)
With srs
vYVals = .Values
'vXVals = .XValues
' clear existing labels
.HasDataLabels = False
For iPts = .Points.Count To 1 Step -1
If Not IsEmpty(vYVals(iPts)) Then
' add label
srs.Points(iPts).ApplyDataLabels _
ShowSeriesName:=False, _
ShowCategoryName:=False, ShowValue:=True, _
AutoText:=True, LegendKey:=False
Exit For
End If
'Reposition label
If srs.Points(iPts).HasDataLabel Then
With srs.Points(iPts).DataLabel
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlTop
.ReadingOrder = xlLTR
.Position = xlLabelPositionAbove
.Orientation = xlHorizontal
End With
End If
'Adjust label size and colour
If srs.Points(iPts).HasDataLabel Then
With srs.Points(iPts)
.MarkerSize = 7
.MarkerStyle = xlCircle
.MarkerBackgroundColorIndex = 6
.MarkerForegroundColorIndex = 1
End With
End If
Next
End With
Next
' legend is now unnecessary
Application.ScreenUpdating = True
End If
End Sub
这部分似乎没有得到应用
'Reposition label
If srs.Points(iPts).HasDataLabel Then
With srs.Points(iPts).DataLabel
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlTop
.ReadingOrder = xlLTR
.Position = xlLabelPositionAbove
.Orientation = xlHorizontal
End With
End If
'Adjust label size and colour
If srs.Points(iPts).HasDataLabel Then
With srs.Points(iPts)
.MarkerSize = 7
.MarkerStyle = xlCircle
.MarkerBackgroundColorIndex = 6
.MarkerForegroundColorIndex = 1
End With
End If
如果我删除 Exit For
,则上述格式将应用于所有数据点
Sub LastPointLabel2()
Dim srs As Series
Dim iPts As Long
Dim cht As ChartObject
Dim vYVals As Variant
Dim vXVals As Variant
Set ws = ActiveSheet
If ActiveChart Is Nothing Then
MsgBox "Select a chart and try again.", vbExclamation
Else
Application.ScreenUpdating = False
For Each cht In ws.ChartObjects
Set srs = cht.Chart.SeriesCollection(1)
With srs
vYVals = .Values
'vXVals = .XValues
' clear existing labels
.HasDataLabels = False
For iPts = .Points.Count To 1 Step -1
If Not IsEmpty(vYVals(iPts)) Then
' add label
srs.Points(iPts).ApplyDataLabels _
ShowSeriesName:=False, _
ShowCategoryName:=False, ShowValue:=True, _
AutoText:=True, LegendKey:=False
'Reposition label
If srs.Points(iPts).HasDataLabel Then
With srs.Points(iPts).DataLabel
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlTop
.ReadingOrder = xlLTR
.Position = xlLabelPositionAbove
.Orientation = xlHorizontal
End With
End If
'Adjust label size and colour
If srs.Points(iPts).HasDataLabel Then
With srs.Points(iPts)
.MarkerSize = 7
.MarkerStyle = xlCircle
.MarkerBackgroundColorIndex = 6
.MarkerForegroundColorIndex = 1
End With
End If
Exit For
End If
Next
End With
Next
' legend is now unnecessary
Application.ScreenUpdating = True
End If
End Sub