카테고리 없음
[Excel][VBA] 차트 레이블 추가
어린왕자1234
2021. 11. 26. 19:15
Sub DataLabelsFromRange() Dim DLRange As Range Dim Cht As Chart Dim i As Integer, Pts As Integer ' Specify chart Set Cht = ActiveSheet.ChartObjects(1).Chart ' Prompt for a range On Error Resume Next Set DLRange = Application.InputBox _ (prompt:="Range for data labels?", Type:=8) If DLRange Is Nothing Then Exit Sub On Error GoTo 0 ' Add data labels Cht.SeriesCollection(1).ApplyDataLabels _ Type:=xlDataLabelsShowValue, _ AutoText:=True, _ LegendKey:=False ' Loop through the Points, and set the data labels Pts = Cht.SeriesCollection(1).Points.Count For i = 1 To Pts 'Cht.SeriesCollection(1). _ Points(i).DataLabel.Text = DLRange(i) 'Use the statement below for formulas Cht.SeriesCollection(1).Points(i).DataLabel.Text = _ "=" & "'" & DLRange.Parent.Name & "'!" & _ DLRange(i).Address(ReferenceStyle:=xlR1C1) Next i End Sub Sub RemoveDataLabels() Dim Cht As Chart Dim x As Series ' Specify chart Set Cht = ActiveSheet.ChartObjects(1).Chart Cht.SeriesCollection(1).HasDataLabels = False End Sub |