How to make VBA code graphing sheet independent

Help1468

New Member
Joined
Jun 1, 2021
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I am very new to excel VBA so pardon my ignorance. I am trying to graph a set of values in a scatter plot. I have got the code to work on the sheet I made the code on, but if I run it on another sheet it only plots the data from the original sheet. Here is my current code:

Sub GraphStressStrainCurve()
'
' GraphStressStrainCurve Macro
'
' Keyboard Shortcut: Ctrl+Shift+Q
'
Range("O8").Select
ActiveSheet.Shapes.AddChart2(240, xlXYScatterSmoothNoMarkers, 480, 57.5, 432).Select
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(1).Name = "=""Title"""
ActiveChart.FullSeriesCollection(1).XValues = "='Sheet1'!$E$13:$E$10000"
ActiveChart.FullSeriesCollection(1).Values = "='Sheet1'!$D$13:$D$10000"

ActiveChart.Axes(xlCategory).MinimumScale = 0
ActiveChart.Axes(xlCategory).MaximumScale = 600
ActiveChart.Axes(xlValue).MinimumScale = 0
ActiveChart.Axes(xlValue).MaximumScale = 1

End Sub



I assume the issue is with the part where it calls out 'Sheet 1', but I don't know how to fix it. Ideally the code would just run on whatever sheet I was on. I have read about ActiveSheet, but I don't know how to implement it.

Thanks!
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

Forum statistics

Threads
1,214,383
Messages
6,119,198
Members
448,874
Latest member
Lancelots

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