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!
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
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.
 
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0
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>
 
Upvote 0

Forum statistics

Threads
1,213,501
Messages
6,114,010
Members
448,543
Latest member
MartinLarkin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top