VBA to add Multiple Charts on One Worksheet

BBeginner

New Member
Joined
Sep 14, 2023
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hi all,
I am wanting to use VBA to insert multiple charts into one worksheet in specific spaces.
I have a chart template created already as the same chart will be used for each.
Here is a photo to help explain what I'm wanting to do. I want to use the data in each row to create a bar chart for each studentID. I want the chart to be placed with the top left corner directly under the studentID.
I have around 2000 charts that need to be created. Any help is greatly appreciated!
1694711529600.png
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
This macro operates on the active sheet and uses the first chart on that sheet as the chart template. It copies the chart template and places it below each StudentID row and sets the chart name, title and source data using that student's data.

Rather than adding all 2000 charts, for test purposes the macro loop is For studentRow = 2 To 50 Step 13, so adding only 4 charts. To add all 2000 charts, comment out or delete that line and remove the comment character at the start of 'For studentRow = 2 To lastStudentRow Step 13.

VBA Code:
Public Sub Add_Student_Charts()

    Dim studentRow As Long, lastStudentRow As Long
    Dim chartTemplate As ChartObject
    Dim studentChart As ChartObject
    
    Application.ScreenUpdating = False
    
    With ActiveSheet
        Set chartTemplate = .ChartObjects(1)
        lastStudentRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        For studentRow = 2 To 50 Step 13                'Testing - only 4 students
        'For studentRow = 2 To lastStudentRow Step 13   'All students
            Set studentChart = chartTemplate.Duplicate.Chart.Parent
            studentChart.Top = .Range("A" & studentRow + 1).Top
            studentChart.Left = .Range("A" & studentRow + 1).Left
            studentChart.Name = "Chart Student " & .Range("A" & studentRow).Value
            studentChart.Chart.SetSourceData Source:=.Range("B1:F1,B" & studentRow & ":F" & studentRow)
            studentChart.Chart.ChartTitle.Text = .Range("A" & studentRow).Value
        Next
    End With
    
    Application.ScreenUpdating = True
    
End Sub

Run this macro if you need to delete all the student charts, leaving only the chart template (the first chart on the sheet):
VBA Code:
Public Sub Delete_Student_Charts()

    Dim i As Long

    With ActiveSheet
        For i = .ChartObjects.Count To 2 Step -1
            .ChartObjects(i).Delete
        Next
    End With
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,948
Messages
6,175,579
Members
452,653
Latest member
craigje92

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top