Nanashi的回复帮助我修改了下面的代码;图表现在响应正常。想要进一步调整,以便每次循环步进到下一个系列时strfieldrange将向右偏移一列(例如,$ Z $ 4:$ Z $ 159应该变为$ AA $ 4:$ AA $ 159,在第二步中环)。我正在学习,请原谅粗糙的边缘。
Sub stacklabels2()
finalrow = Worksheets("Stacking RR").Range("Z" & Rows.Count).End(xlUp).Row 'trying to derive the proper range dynamically
' sensing that I need another variable here that will allow me to cause strfieldrange to step with the loop
strfieldrange = "=" & "'Stacking RR'!" & Range("$Z$4:$Z$159").Address
Sheets("Stacking").Select
ActiveSheet.ChartObjects("StackingPlan").Activate
ActiveChart.SetElement (msoElementDataLabelNone)
ActiveChart.ApplyDataLabels
ActiveSheet.ChartObjects("StackingPlan").Activate
For Each Ser In ActiveChart.SeriesCollection
Ser.DataLabels.Format.TextFrame2.TextRange.InsertChartField msoChartFieldRange, strfieldrange, 0
Ser.DataLabels.ShowRange = True
Ser.DataLabels.ShowValue = False
Next Ser
End Sub
有什么想法吗?感谢。
修改
我想我解决了。将以下代码替换为上面的较大宏:
keycolumn = Range("Z4").Column
For Each Ser In ActiveChart.SeriesCollection
n = Val(Ser.Name)
Ser.DataLabels.Format.TextFrame2.TextRange.InsertChartField msoChartFieldRange, CStr("='Stacking RR'!" & Range(Cells(4, keycolumn + n), Cells(LastRow, keycolumn + n)).Address), 0
Ser.DataLabels.ShowRange = True
Ser.DataLabels.ShowValue = False
Next Ser
真棒。感谢@nanashi的所有帮助。
答案 0 :(得分:1)
它采用原始公式字符串,如" ="在一开始的时候。尝试这样做:
Sub Blah()
' Other code.
StrFieldRange = "=" & Range("A1").Resize(10, 1).Address
ActiveSheet.ChartObjects("Chart1").Activate
For Each Ser In ActiveChart.SeriesCollection
Ser.DataLabels.Format.TextFrame2.TextRange.InsertChartField _
msoChartFieldRange, StrFieldRange, 0
Next
'Etc.
End Sub
这相当于使用:=$A$1:$A$10
。如果这有帮助,请告诉我们。
修改强>
根据讨论:
Sub StackLabelsMod()
Dim WS0 As Worksheet, WS1 As Worksheet
Dim RngHeader As Range, RngCell As Range
Dim StrField As String
Dim LastRow As Long, ResizeRow As Long
With ThisWorkbook
Set WS0 = .Sheets("Stacking RR")
Set WS1 = .Sheets("Stacking")
End With
With WS0
LastRow = .Range("Z" & .Rows.Count).End(xlUp).Row
ResizeRow = LastRow - 3 'If data begins at row 4, subtract 1 to get correct starting row of data.
Set RngHeader = .Range("Z4:AS4")
End With
For Each RngCell In RngHeader
StrField = "='Stacking RR'!" & RngCell.Resize(ResizeRow, 1).Address
WS1.ChartObjects("StackingPlan").Activate
ActiveChart.SetElement (msoElementDataLabelNone)
ActiveChart.ApplyDataLabels
ActiveSheet.ChartObjects("StackingPlan").Activate
For Each Ser In ActiveChart.SeriesCollection
Ser.DataLabels.Format.TextFrame2.TextRange.InsertChartField msoChartFieldRange, StrField, 0
Ser.DataLabels.ShowRange = True
Ser.DataLabels.ShowValue = False
Next Ser
Next
End Sub
如果有帮助,请告诉我。
答案 1 :(得分:0)
我不确定我是否理解你的问题。
然而,
尝试替换"=$A$1:$A$15"
Range("A1:A15").Resize(10)
答案 2 :(得分:0)
工作代码:
Sub StackLabels()
Dim WS0 As Worksheet, WS1 As Worksheet
Dim RngHeader As Range, RngCell As Range
Dim StrField As String
Dim LastRow As Long, ResizeRow As Long
With ThisWorkbook
Set WS0 = .Sheets("Stacking RR")
Set WS1 = .Sheets("Stacking")
End With
With WS0
LastRow = .Range("Z" & .Rows.Count).End(xlUp).Row
ResizeRow = LastRow - 3 'If data begins at row 4, subtract 1 to get correct starting row of data.
Set RngHeader = .Range("Z4:AS4")
End With
WS1.ChartObjects("StackingPlan").Activate
ActiveChart.SetElement (msoElementDataLabelNone)
ActiveChart.ApplyDataLabels
ActiveSheet.ChartObjects("StackingPlan").Activate
keycolumn = Range("Z4").Column
For Each Ser In ActiveChart.SeriesCollection
n = Val(Ser.Name)
Ser.DataLabels.Format.TextFrame2.TextRange.InsertChartField msoChartFieldRange, CStr("='Stacking RR'!" & Range(Cells(4, keycolumn + n), Cells(LastRow, keycolumn + n)).Address), 0
Ser.DataLabels.ShowRange = True
Ser.DataLabels.ShowValue = False
Next Ser
End Sub