rodwhiteley
New Member
- Joined
- Jan 15, 2012
- Messages
- 37
Hey,
VBA newbie here, sorry.
TLDR: Macro to create 3 scatter plots works OK, but only for data from the sheet it was created.
How do I change the macro to refer to the current sheet, or alternately cycle through and repeat for each of the 36 sheets in the work book?
In full:
I have some experimental data, where we collected some muscle activity in 36 different experimental conditions, for 6 muscles each time. I now want to graph these as 3 X 36 Scatter plots (each scatter showing 2 muscles). Of course I could do this manually one by one, but this seems like a process that should be automated with a macro.
I used the Macro recorder to create an XY scatter plot with 2 series, 3 times each on a worksheet, ultimately getting this to work OK, including some customisation of the axes and adding a legend that I need. (Couldn't get it to work adding custom error bars using the SD's - question for another time)
However I want to now run the Macro on each of the 36 sheets in the workbook, but the macro has hardcoded the sheet name, so running the macro in another sheet pulls the data from the original sheet I recorded the macro in (in the example below, the sheet is named '(7)' )
My guess is that I need to change these absolute references to a reference to the Active Sheet, but this is beyond me at the moment.
The 36 sheets in the workbook are named (1) (2) (3) ... (36), and the data is set up exactly the same in each sheet - I have a link to some sample data below.
Here is a copy and paste from the Macro Editor that works OK on sheet (7):
Here's a link to a sample sheet from the main dataset:
https://onedrive.live.com/redir?res...576&authkey=!AKV8rKmmrYTA_I8&ithint=file,xlsx
Thanks in advance for any help,
Rod
VBA newbie here, sorry.
TLDR: Macro to create 3 scatter plots works OK, but only for data from the sheet it was created.
How do I change the macro to refer to the current sheet, or alternately cycle through and repeat for each of the 36 sheets in the work book?
In full:
I have some experimental data, where we collected some muscle activity in 36 different experimental conditions, for 6 muscles each time. I now want to graph these as 3 X 36 Scatter plots (each scatter showing 2 muscles). Of course I could do this manually one by one, but this seems like a process that should be automated with a macro.
I used the Macro recorder to create an XY scatter plot with 2 series, 3 times each on a worksheet, ultimately getting this to work OK, including some customisation of the axes and adding a legend that I need. (Couldn't get it to work adding custom error bars using the SD's - question for another time)
However I want to now run the Macro on each of the 36 sheets in the workbook, but the macro has hardcoded the sheet name, so running the macro in another sheet pulls the data from the original sheet I recorded the macro in (in the example below, the sheet is named '(7)' )
My guess is that I need to change these absolute references to a reference to the Active Sheet, but this is beyond me at the moment.
The 36 sheets in the workbook are named (1) (2) (3) ... (36), and the data is set up exactly the same in each sheet - I have a link to some sample data below.
Here is a copy and paste from the Macro Editor that works OK on sheet (7):
Code:
Sub Charts()
'
' Charts Macro
' Create 3 scatter plots
'
'
Range(Selection, Cells(ActiveCell.Row, 1)).Select
Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlToLeft)).Select
Range("A2:B101").Select
ActiveSheet.Shapes.AddChart2(240, xlXYScatter).Select
ActiveChart.SetSourceData Source:=Range("'(7)'!$A$2:$B$101")
ActiveChart.Axes(xlValue).Select
ActiveChart.Axes(xlValue).MinimumScale = 0
ActiveChart.Axes(xlValue).MaximumScale = 1
Selection.TickLabels.NumberFormat = "0.00%"
Selection.TickLabels.NumberFormat = "0%"
ActiveChart.Axes(xlCategory).Select
ActiveChart.Axes(xlCategory).MinimumScale = 0
ActiveChart.Axes(xlCategory).MaximumScale = 100
ActiveChart.FullSeriesCollection(1).Name = "='(7)'!$B$1"
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(2).Name = "='(7)'!$D$1"
ActiveChart.FullSeriesCollection(2).XValues = "='(7)'!$A$2:$A$101"
ActiveChart.FullSeriesCollection(2).Values = "='(7)'!$D$2:$D$101"
ActiveChart.ChartTitle.Select
Selection.Delete
ActiveChart.SetElement (msoElementLegendTop)
Range("A1").Select
Selection.End(xlDown).Select
Range(Selection, Selection.End(xlUp)).Select
Range("A2:A101,F2").Select
Range("F2").Activate
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Shapes.AddChart2(240, xlXYScatter).Select
ActiveChart.SetSourceData Source:=Range("'(7)'!$A$2:$A$101,'(7)'!$F$2:$F$101" _
)
ActiveChart.Axes(xlValue).Select
ActiveChart.Axes(xlValue).MinimumScale = 0
ActiveChart.Axes(xlValue).MaximumScale = 1
Selection.TickLabels.NumberFormat = "0.00%"
Selection.TickLabels.NumberFormat = "0%"
ActiveChart.Axes(xlCategory).Select
ActiveChart.Axes(xlCategory).MinimumScale = 0
ActiveChart.Axes(xlCategory).MaximumScale = 100
ActiveChart.FullSeriesCollection(1).Name = "='(7)'!$F$1"
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(2).Name = "='(7)'!$H$1"
ActiveChart.FullSeriesCollection(2).XValues = "='(7)'!$A$2:$A$101"
ActiveChart.FullSeriesCollection(2).Values = "='(7)'!$H$2:$H$101"
ActiveChart.ChartTitle.Select
Selection.Delete
ActiveChart.SetElement (msoElementLegendTop)
Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveWindow.ScrollRow = 76
ActiveWindow.ScrollRow = 67
ActiveWindow.ScrollRow = 6
ActiveWindow.ScrollRow = 1
Range("A2:A101,J2").Select
Range("J2").Activate
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Shapes.AddChart2(240, xlXYScatter).Select
ActiveChart.SetSourceData Source:=Range("'(7)'!$A$2:$A$101,'(7)'!$J$2:$J$101" _
)
ActiveChart.Axes(xlValue).Select
ActiveChart.Axes(xlValue).MinimumScale = 0
ActiveChart.Axes(xlValue).MaximumScale = 1
Selection.TickLabels.NumberFormat = "0.00%"
Selection.TickLabels.NumberFormat = "0%"
ActiveChart.Axes(xlCategory).Select
ActiveChart.Axes(xlCategory).MinimumScale = 0
ActiveChart.Axes(xlCategory).MaximumScale = 100
ActiveChart.FullSeriesCollection(1).Name = "='(7)'!$J$1"
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(2).Name = "='(7)'!$L$1"
ActiveChart.FullSeriesCollection(2).XValues = "='(7)'!$A$2:$A$101"
ActiveChart.FullSeriesCollection(2).Values = "='(7)'!$L$2:$L$101"
ActiveChart.ChartTitle.Select
Selection.Delete
ActiveChart.SetElement (msoElementLegendTop)
End Sub
Here's a link to a sample sheet from the main dataset:
https://onedrive.live.com/redir?res...576&authkey=!AKV8rKmmrYTA_I8&ithint=file,xlsx
Thanks in advance for any help,
Rod