Hi all,
I have a spreadsheet of car park occupancy for each hour of the day across 5 car parks. The first 24 rows are hourly occupancies for Car Park A, the next for Car Park B, and so on.
I want a chart of hourly occupancy for each of the 5 car parks in the spreadsheet and automate this process.
I know I'd need a for loop which creates the first chart and then adds 24 rows to the chart's data array, but struggling to get my head around this.
The code I have so far is as follows:
Sub BuildCharts()
Dim x As Integer
For x = 0 To 5
ActiveSheet.Shapes.AddChart2(227, xlLine).Select 'add chart (specific type)
ActiveChart.SeriesCollection.NewSeries 'create data series
ActiveChart.FullSeriesCollection(1).Values = "='2022 Occupancy'!$B$2:$B$25" 'series data for 1st car park is C2:C25, but each car park will have to add 24 rows to the series data array
ActiveChart.FullSeriesCollection(1).XValues = "='2022 Occupancy'!$C$2:$C$25" 'X Axis (24 rows of hourly increments) could do with incrementing by 24 rows as well for consistency
ActiveChart.ChartTitle.Select
Selection.Caption = "='2022 Occupancy'!R2C1" 'Set chart title to car park name (first cell in column A, but needs to increment by 24 rows as well as each 24 rows, there is a new car park)
ActiveChart.SetElement (msoElementPrimaryCategoryAxisTitleAdjacentToAxis)
Application.CutCopyMode = False
Selection.Caption = "='2022 Occupancy'!R1C3" 'Set X axis to 'Time Periods' (C1)
ActiveChart.SetElement (msoElementPrimaryValueAxisTitleAdjacentToAxis)
Application.CutCopyMode = False
Selection.Caption = "='2022 Occupancy'!R1C2" 'Set Y axis to 'Occupancy' (B1)
ActiveChart.FullSeriesCollection(1).Select
With Selection.Format.Line 'Format line as red
.Visible = msoTrue
.ForeColor.RGB = RGB(255, 0, 0)
.Transparency = 0
End With
x = x + 24
Next x
End Sub
Any help would be hugely appreciated
Bookmarks