0

I am creating charts using data from a tabs called Team1-Team8. I am creating the charts for each team ok but I can't get the charts into each team tab called "Team a - Charts". Below is the code i have so far for just Team A.My Parameter sheet, Column B has the names of the tabs for the charts and Column A is the Team names. Any pointers would help.

Sub LooproutineCharts()

Dim TeamName As String
Dim TeamNameCharts As String

For i = 4 To 12

TeamName = Sheets("Parameter").Range("A" & i).Value 'identify the location

TeamNameCharts = Sheets("Parameter").Range("B" & i).Value 'identify the location

Call Charts(TeamName) ' Call subroutine

Call Charts(TeamNameCharts) ' Call subroutine

Next i

End Sub

Sub Charts(TeamName As String)

'Create a Line Chart for Healthy Start Docu'

Dim lastRow As Long
Dim ws As Worksheet
Set ws = Sheets(TeamName)

 With Sheets(TeamName)
    lastRow = .Range("U" & Rows.count).End(xlUp).Row
With ws
    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xlLineMarkers
    ActiveChart.Parent.Name = "Variable A"
    ActiveChart.SetSourceData Source:=.Range("S3:U" & lastRow)

ActiveSheet.Shapes("Variable A").Top = 20
ActiveSheet.Shapes("Variable A").Left = 20
ActiveSheet.Shapes("Variable A").Height = 300
ActiveSheet.Shapes("Variable A").Width = 700

ActiveChart.HasTitle = True
ActiveChart.ChartTitle.Text = "Variable A" TeamName"



    End With
    End With

    End Sub
4
  • in your Charts Sub within the "with ws", you are referencing the activesheet, which may well be different from the ws, so as a first measure you could try to remove the activesheet reference, so it just reads ".Shapes.addChart.Select" Commented Jun 10, 2016 at 10:26
  • thanks for the reply. I get a read only property error on the line "Parent.Name = "Variable A"" Commented Jun 10, 2016 at 10:42
  • You are also using it in the ChartTitle (though the syntax is off), but is the "Variable A" intended for use other than referencing the shape? Commented Jun 10, 2016 at 11:07
  • Variable A is just a reference for the name of the chart Commented Jun 10, 2016 at 11:48

1 Answer 1

2

I would suggest updating the Charts Sub to make use of Worksheet.ChartObjects

Using the ChartObject, you can set it and won't have to reference a shape by name. It would look like this:

Sub Charts(TeamName As String)
    'Create a Line Chart for Healthy Start Docu'
    Dim theChart As ChartObject
    Dim lastRow As Long
    Dim ws As Worksheet
    Set ws = Sheets(TeamName)

    With ws
        lastRow = .Range("U" & Rows.Count).End(xlUp).Row

        Set theChart = .ChartObjects.Add(Left:=20, Top:=20, Width:=700, Height:=300)
        With theChart.Chart
            .ChartType = xlLineMarkers
            .SeriesCollection.Add Source:=ws.Range("S3:U" & lastRow)
            '.SeriesCollection(1).XValues = ws.Range("S2:U2") 'I have no idea where your xaxis is placed, or if it exist
            .HasTitle = True
            .ChartTitle.Text = TeamName
        End With
    End With

End Sub

I've taken the liberty to assume that the chart title should match the TeamName argument. I've also made it ready for the xAxis, but I have no oidea if it is relevant, or where it is placed

Sign up to request clarification or add additional context in comments.

2 Comments

That has placed the charts in to the correct tabs! thanks!
Why is the question -1?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.