Hi everyone
I've just signed up, so apologies if I do something wrong here!!
I'm trying to write some VBA code to produce 8 charts based on 8 rows of data:
data.png
The code, so far, looks like this:
Option Explicit
Sub chart()
Dim invalidType, user, reportTitle As String
Dim i, userCount, userPos As Integer
Dim plotLabels As Range
Dim plotData As Range
Dim startingCell As String
startingCell = ActiveCell.Address
invalidType = ActiveCell.Text
Set plotLabels = Range(Selection.Offset(2, 1), Selection.Offset(2, 12))
userCount = 8
For i = 1 To userCount
Range(startingCell).Select
userPos = 2 + i
user = ActiveCell.Offset(userPos, 0).Text
reportTitle = user & " - " & invalidType
Set plotData = Range(Selection.Offset(userPos, 1), Selection.Offset(userPos, 12))
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData Source:=Range(plotLabels, plotData)
Next
End Sub
This code almost works. The idea is that I start with cell A1 selected (Data Type) and then when I run the code, it produces 8 charts, however, each chart has an extra series plotted, i.e. the first chart correctly plots the data specified in the range B4:M4, but the second chart plots data for the range B4:M5. This carries on throught the remaining charts, ending with the eighth chart having all 8 sets of data plotted (B4:M11).
What I am trying to achieve is to have each chart plotting only one set of data, i.e the first chart should plot B4:M4, the second should plot B5:M5, the third should plot B6:M6, etc.
I think the problem lies in the way I am specifying the source data in the line:
ActiveChart.SetSourceData Source:=Range(plotLabels, plotData)
but I don't know how else to specify both the data labels on the axis and the actual data.
I have also attached a sample spreadsheet, with the 'chart' macro so you can see how it currently works. Any suggestions that anyone can come up with would be greatly appreciated.
Bookmarks