The aim is to plot the values against column A
(X-axis). The user selects a column name in cell G4
, the values of G4
could be B
,C
,C
,D
. Based on the column name a chart has to be plotted.
I was able to write the following code which selects the data but I cannot select the data dynamically.
Sub Chart()
Dim Lastrow As Long
Dim TimeAxis As Range
Dim Values As Range
Dim cht As Object
Lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
TimeAxis = Range("A1:A" & Lastrow).Select '<----- X Axis (Static)
Values = Range("B1:B" & Lastrow).Select '<----- User selected as per cell G4 (Dynamic)
'Plot TimeAxis Vs Values
Set cht = ActiveSheet.Shapes.AddChart2
cht.Chart.SetSourceData Source:=rng '<------ User Range (Dynamic)
cht.Chart.ChartType = xlXYScatterLines
End Sub