Create a chart with data from multiple sheets

Status
Not open for further replies.

MartinDH

New Member
Joined
Oct 16, 2018
Messages
3
Hello,

I am new at VBA and I definetly need some help.
I am trying to create a XY graph with data from multiple sheets.
All the sheets have the same structure and different names. The number of sheets can vary from one file to another.

I recorded the macro of what I need but it is only working on this specific case (number of sheet = 3 and specific names).
I want to create a loop that can go through all the sheets of my document and create the plot.
Finally I want to move the graph to a new sheet at the end of the document.

Can anybody help me ?

Sub Macro8()
'
' Macro8 Macro
'


'
ActiveSheet.Shapes.AddChart2(240, xlXYScatterSmoothNoMarkers).Select
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(1).Name = "='Sheet1'!$B$6"
ActiveChart.FullSeriesCollection(1).XValues = "='Sheet1'!$F$31:$F$100"
ActiveChart.FullSeriesCollection(1).Values = "='Sheet1'!$G$31:$G$100"
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(2).Name = "='Sheet2'!$B$6"
ActiveChart.FullSeriesCollection(2).XValues = "='Sheet2'!$F$31:$F$100"
ActiveChart.FullSeriesCollection(2).Values = "='Sheet2'!$G$31:$G$100"
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(3).Name = "='Sheet3'!$B$6"
ActiveChart.FullSeriesCollection(3).XValues = "='Sheet3'!$F$31:$F$100"
ActiveChart.FullSeriesCollection(3).Values = "='Sheet3'!$G$31:$G$100"
ActiveChart.Axes(xlCategory).MaximumScale = 1
ActiveChart.SetElement (msoElementLegendRight)
ActiveChart.Location Where:=xlLocationAsNewSheet, Name:="Chart"
Sheets("Chart").Select
Sheets("Chart").Move After:=Sheets(5)


End Sub

Thanks in advance
Martin
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Try the following...

Code:
Option Explicit

Sub CreateXYChart()


    Dim XYChart As Chart
    Dim currentSeries As Series
    Dim currentWorksheet As Worksheet
    
    'Check whether a sheet is active
    If TypeName(ActiveSheet) = "Nothing" Then
        MsgBox "No sheet is available!", vbExclamation
        Exit Sub
    End If
    
    'Add chart to the active sheet
    Set XYChart = ActiveSheet.Shapes.AddChart2(Style:=240, XlChartType:=xlXYScatterSmoothNoMarkers).Chart
    
    'Remove extra series, if any
    With XYChart
        Do While .SeriesCollection.Count > 0
            .SeriesCollection(1).Delete
        Loop
    End With
    
    'Loop through all worksheets in the active workbook
    For Each currentWorksheet In Worksheets
        'Add a new series to the chart
        Set currentSeries = XYChart.SeriesCollection.NewSeries
        'Name and assign XY values to the series from the current worksheet
        With currentSeries
            .Name = "='" & currentWorksheet.Name & "'!$B$6"
            .XValues = "='" & currentWorksheet.Name & "'!$F$31:$F$100"
            .Values = "='" & currentWorksheet.Name & "'!$G$31:$G$100"
        End With
    Next currentWorksheet
    
    With XYChart
        .Axes(xlCategory).MaximumScale = 1
        .SetElement (msoElementLegendRight)
        .Location Where:=xlLocationAsNewSheet, Name:="Chart"
    End With
    
    ActiveSheet.Move after:=Sheets(Sheets.Count)
    
End Sub

Hope this helps!
 
Upvote 0
Hi im trying to make something like this but whit this two differences

First one
My X column and Y column are related to a third column. This third column needs to satisfy a minor an mayor of

Second one
My name of series need to be the name of the sheet they were extracted

Thanks
 
Upvote 0
Duplicate to: Plot many sheets in scatter chart

Please stop posting the same question multiple times. Per Forum Rules (#12), posts of a duplicate nature will be locked or deleted.

In relation to your question here, I have closed this thread so please continue in the linked thread. If you do not receive a response, you can "bump" it by replying to it yourself, though we advise you to wait 24 hours before doing so, and not to bump a thread more than once a day.
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,214,943
Messages
6,122,376
Members
449,080
Latest member
Armadillos

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