Needed Macro Code to create graphs

Rampage598

New Member
Joined
Mar 11, 2022
Messages
23
Office Version
  1. 365
Platform
  1. Windows
Excel File Download Link

1702566351049.png


I'm looking to generate a 2D line graph across multiple worksheets with varying amounts of row data. I need help creating the same type of graph depicted in an image for each worksheet. Could someone assist me in developing a macro code to achieve this task across all sheets?

I have attached my Excel file download link,

I'd like to replicate two manually created graphs found in the "23104137" sheet across all other sheets in the Excel file. Could someone assist me by providing a macro code to achieve this?
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
First, you probably ought to use XY Scatter charts, which plot the date/time values as numbers, not nonnumeric labels. But that's up to you.

I would start with something like this. You will have to modify formatting of the charts, of course.

VBA Code:
Sub ChartsOnEachWorksheet()
  Dim ws As Worksheet
  For Each ws In ActiveWorkbook.Worksheets
    If IsNumeric(ws.Name) Then
      Dim rng As Range
      Set rng = ws.Range("A1").CurrentRegion
      ' first chart
      Dim r1source As Range ' source data
      Set r1source = rng.Resize(, 4)
      Dim r1tlc As Range ' top left cell
      Set r1tlc = ws.Range("I2")
      Dim cht1 As Chart
      Set cht1 = ws.Shapes.AddChart2(, xlXYScatterLinesNoMarkers, r1tlc.Left, r1tlc.Top).Chart
      cht1.SetSourceData r1source
      cht1.SeriesCollection(1).AxisGroup = xlSecondary
      ' second chart
      Dim r2source As Range
      Set r2source = Union(rng.Resize(, 1), rng.Resize(, 2).Offset(, 4))
      Dim r2tlc As Range ' top left cell
      Set r2tlc = ws.Range("I17")
      Dim cht2 As Chart
      Set cht2 = ws.Shapes.AddChart2(, xlXYScatterLinesNoMarkers, r2tlc.Left, r2tlc.Top).Chart
      cht2.SetSourceData r2source
    End If
  Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,876
Members
452,363
Latest member
merico17

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