Add Vertical Line to Graph

iliauk

Board Regular
Joined
Jun 3, 2014
Messages
163
Hi All, I am trying to add a vertical line (via macro) to my graph and can't quite grasp it.

This code adds the vertical line but changes the whole graph to scatter (which mucks up my x-axis)

Code:
Sub VertLine2()
Dim ChartName As String
Dim Labels As String
Dim vntArray As Variant
Dim maxinput As Long


dateinput = 37803


maxinput = ActiveChart.Axes(xlValue).MaximumScale
ActiveChart.Axes(xlValue).MaximumScale = maxinput
ActiveChart.ChartType = xlXYScatterLinesNoMarkers


With ActiveChart.SeriesCollection.NewSeries
        .Name = "Line"
        .XValues = Array(dateinput, dateinput)
        .Values = Array(0, maxinput)
        .Format.Line.Visible = msoTrue
        .Format.Line.ForeColor.ObjectThemeColor = msoThemeColorText1
        .MarkerStyle = -4142
End With
End Sub

This code should do the trick (only change the new series to scatter)

Code:
Sub VertLine()
'Dim arLabels As String
'Dim exLabels As String
'Dim lablerange As Range
'Dim yarray() As Variant
Dim ChartName As String
Dim maxinput As Long
Dim dateinput As Long


'Fix the y-axis so that line cuts through
maxinput = ActiveChart.Axes(xlValue).MaximumScale
ActiveChart.Axes(xlValue).MaximumScale = maxinput
' Enter date at which to add line
dateinput = 37803


'''Get x-values'''
'arLabels = ActiveChart.SeriesCollection(1).Formula
'exLabels = Mid(Mid(arLabels, InStr(arLabels, ",") + 1), 1, InStr(1, Mid(arLabels, InStr(arLabels, ",") + 2), ","))
'Debug.Print exLabels
'Set LabelRange = Range(exLabels)
''Create corresponding array
'counter = 1
'For Each cell In LabelRange
'    ReDim Preserve yarray(1 To counter)
'        If cell = dateinput Then
'        yarray(counter) = maxinput
'        End If
'        counter = counter + 1
'Next cell
'''Get x-values'''
'Insert line
ActiveChart.SeriesCollection.NewSeries.Name = "VertLine"
'Change to scatter
With ActiveChart.SeriesCollection(ActiveChart.SeriesCollection.Count)
    .ChartType = xlXYScatterLinesNoMarkers
    .XValues = Array(dateinput)
    .Values = "={0,1000}"
    .Format.Line.Visible = msoTrue
    .Format.Line.ForeColor.ObjectThemeColor = msoThemeColorText1
    .MarkerStyle = -4142
End With


'Delete legend
ActiveChart.Legend.LegendEntries(ActiveChart.Legend.LegendEntries.Count).Delete
     
End Sub

But it completely destroys the axis

Out of interesting, a naive recording works and leads me to believe that I am defining the Xvalues wrong (even though it works in the first case)

Code:
    ActiveChart.ChartArea.Select
    ActiveChart.PlotArea.Select
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(4).Name = "=""Line"""
    ActiveChart.Legend.Select
    ActiveChart.Legend.LegendEntries(4).Select
    ActiveChart.SeriesCollection(4).ChartType = xlXYScatterLinesNoMarkers
    ActiveChart.PlotArea.Select
    ActiveChart.SeriesCollection(4).XValues = "='C Data'!$A$84,'C Data'!$A$85"
    ActiveChart.SeriesCollection(4).Values = "={0,1000}"

Thanks!
 

Some videos you may like

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

Jon Peltier

MrExcel MVP
Joined
May 14, 2003
Messages
4,920
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
How many points are in the added data series? Two.
How many X and Y values in the recorded macro? Two each.
How many X and Y values in the longer routine? Two Y values but only one X value.
There's your clue.
 

iliauk

Board Regular
Joined
Jun 3, 2014
Messages
163
Hey Jon, sorry my fault. I initially tried:

Code:
.XValues = Array(dateinput, dateinput)
.Values = "={0,1000}"

And that did not work; then I was changing bits and bobs and ended up pasting the version with one entry.
 

iliauk

Board Regular
Joined
Jun 3, 2014
Messages
163
For example, this is the code that I think should work but doesn't

Code:
Dim ChartName As String
Dim maxinput As Long
Dim dateinput As Long

'Fix the y-axis so that line cuts through
maxinput = ActiveChart.Axes(xlValue).MaximumScale
ActiveChart.Axes(xlValue).MaximumScale = maxinput
' Enter date at which to add line
dateinput = 37803
'Insert line
ActiveChart.SeriesCollection.NewSeries.Name = "VertLine"
'Delete legend
ActiveChart.Legend.LegendEntries(ActiveChart.Legend.LegendEntries.Count).Delete
'Formatting
ActiveChart.Axes(xlCategory).TickLabels.NumberFormat = "[$-409]mmm-yy;@"
'Add line
With ActiveChart.SeriesCollection(ActiveChart.SeriesCollection.Count)
    .ChartType = xlXYScatterLinesNoMarkers
    .XValues = Array(dateinput, dateinput)
    .Values = "={0,1000}"
    .Format.Line.Visible = msoTrue
    .Format.Line.ForeColor.ObjectThemeColor = msoThemeColorText1
    .MarkerStyle = -4142
End With
 

iliauk

Board Regular
Joined
Jun 3, 2014
Messages
163
Ok I've kind of solve it, not ideal but it works when I use range.

.ChartType = xlXYScatterLinesNoMarkers
'.XValues = Array(dateinput, dateinput)
.XValues = "=Sheet1!$B$2:$C$2"

Which is:
01/04/2003
01/04/2003

<tbody>
</tbody>

or as values:
3771237712

<tbody>
</tbody>
 

Watch MrExcel Video

Forum statistics

Threads
1,109,331
Messages
5,528,056
Members
409,799
Latest member
camronmartin

This Week's Hot Topics

Top