我试图将工作表中的所有图表扩展12个月,以便在月线图上再添加一年。这是我到目前为止所做的。
Sub Chart_Extender()
Dim Rng_Extension As Integer
Dim Series_Formula As String
Dim StartPoint As String
Dim EndPoint As String
Dim CommaSplit As Variant
Dim ColonSplit As Variant
Dim grph As ChartObject
Dim ser As Series
'Determine the length of the extension (in cells)
On Error GoTo BadEntry
Rng_Extension = InputBox( _
"How many cells do you want to extend your chart's series?", _
"Chart Extender")
On Error GoTo 0
For Each grph In ActiveSheet.ChartObjects
For Each ser In grph.Chart.SeriesCollection
Series_Formula = ser.Formula
CommaSplit = Split(Series_Formula, ",")
ColonSplit = Split(CommaSplit(2), ":")
StartPoint = ColonSplit(0)
EndPoint = ColonSplit(1)
EndPoint = Range(EndPoint).Offset(0, Rng_Extension).Address
ser.Values = StartPoint & ":" & EndPoint 'Combine Start and End Point & Set Series = To It
If CommaSplit(1) <> "" Then
ColonSplit = Split(CommaSplit(1), ":")
StartPoint = ColonSplit(0)
EndPoint = ColonSplit(1)
EndPoint = Range(EndPoint).Offset(0, Rng_Extension).Address 'Extended Ending Point Range
ser.XValues = StartPoint & ":" & EndPoint 'Combine Start and End Point & Set Series = To It
End If
End If
Next ser
Next grph
MsgBox "Your chart has been Extended by " & Rng_Extension & " positions."
Exit Sub
End Sub
然而,由于图中也有命名区域,它似乎卡住了,并且在EndPoint = ColonSplit(1)时出现错误,说&#34;下标超出范围&#34;。有没有办法来解决这个问题?还有一种方法可以添加另一个循环,让它遍历工作簿中的每个工作表并扩展所有工作表吗?
答案 0 :(得分:0)
验证用户输入并确定需要扩展图表后,创建工作簿和工作表对象:
Dim oWB As Workbook: Set oWB = ThisWorkbook
Dim oWS As Worksheet
现在在任何FOR
循环之前添加以下循环:
For Each oWS In oWB.Worksheets
' Your FOR loops here
Next
这将遍历工作簿中的所有工作表。
注意:在引用oWS
或Range
Cell