bryanrobson
New Member
- Joined
- Aug 19, 2022
- Messages
- 21
- Office Version
- 365
- Platform
- Windows
Hi everyone.
I have come across this code on the internet. Basically, it extends a graph by a number of positions. Which works great on my existing worksheet. I was just wondering if anyone would be able to amend the code to suit my needs. The code asks how many cells you want to extend the graph by. So, what I want it to do is if I enter 10, I want it to extend the graph at the end by 10, but shorten the graph at the start by 10. or to be prompted for user input to shorten the length by a different amount to how long its extended by.
Many thanks
I have come across this code on the internet. Basically, it extends a graph by a number of positions. Which works great on my existing worksheet. I was just wondering if anyone would be able to amend the code to suit my needs. The code asks how many cells you want to extend the graph by. So, what I want it to do is if I enter 10, I want it to extend the graph at the end by 10, but shorten the graph at the start by 10. or to be prompted for user input to shorten the length by a different amount to how long its extended by.
Many thanks
VBA Code:
Sub Chart_Extender()
'PURPOSE: Extend horizontally all Chart Series in ActiveSheet by X number of columns (can decrease as well)
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
'Loop Through Each chart in the ActiveSheet
For Each grph In ActiveSheet.ChartObjects
For Each ser In grph.Chart.SeriesCollection
'Test to make sure not XY Scatter Plot Series
If ser.ChartType <> 75 Then
'Get range of series
Series_Formula = ser.Formula
'X Axis Values
CommaSplit = Split(Series_Formula, ",") 'Delimit by comma
ColonSplit = Split(CommaSplit(2), ":") 'Delimit 3rd part by colon
StartPoint = ColonSplit(0) 'Starting Point of Range
EndPoint = ColonSplit(1) 'Current Ending Point Range
EndPoint = Range(EndPoint).Offset(0, Rng_Extension).Address 'Extended Ending Point Range
ser.Values = StartPoint & ":" & EndPoint 'Combine Start and End Point & Set Series = To It
'X Axis Labels
If CommaSplit(1) <> "" Then
ColonSplit = Split(CommaSplit(1), ":") 'Delimit 3rd part by colon
StartPoint = ColonSplit(0) 'Starting Point of Range
EndPoint = ColonSplit(1) 'Current Ending Point Range
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
'Completion Message
MsgBox "Your chart has been Extended by " & Rng_Extension & " positions."
Exit Sub
'Error Handling
BadEntry:
MsgBox "Your input must be a whole number, aborting", vbCritical, "Improper Entry"
End Sub
Last edited by a moderator: