I'm trying to generate a line graph using VBA that selects data from alternating columns. I've included a spreadsheet that contains data for staff allocation (Forecast and Actual) over time.
I'd like to be able to create a graph that plots just the forecast columns for the following rows:
"Total Staff"
"Total Allocation"
"Remaining availability"
Alternatively, I'd like to create a graph that plots a comparison of Forecast vs Actual rows:
"Total Staff"
"Total Allocation" and
"Remaining availability".
I'm having difficulties with understanding how to set the correct range.
I'm using the following code but it's throwing an error:
Sub linegraph()
'
' linegraph Macro
'
Dim AltRng As Range
Dim LastCol As Variant
Dim Rng As Range
Dim RngEnd As Range
Dim Wks As Worksheet
Set Wks = ActiveSheet
LastCol = Wks.Cells.Find("*", , xlValues, xlWhole, xlByColumns, xlPrevious, False).Column
'column 5 starting point for forecast data
For C = 5 To LastCol Step 2
Set Rng = Wks.Columns(C)
If AltRng Is Nothing Then Set AltRng = Rng
Set AltRng = Union(AltRng, Rng)
Next C
With ActiveSheet.ChartObjects.Add _
(Left:=100, Width:=375, Top:=75, Height:=225)
.Chart.SetSourceData Source:=Sheets("Sheet1").Range(AltRng)
.Chart.ChartType = xlXYScatterLines
End With
End Sub
thank you for your help
Bookmarks