Sorry for what is probably a newbie question, but I have searched around for hours on the web and haven't found a solution.
I have a workbook with a hundred or so sheets. Each sheet has data in cells A1:F5. I'd like to use a macro that will allow me to go to a sheet and create a column chart. The title of the chart is to be read from cell A8.
I've used Excel to record the following macro:
Sub create_chart()
'
' create_chart Macro
'
' Keyboard Shortcut: Ctrl+o
'
Range("A1:F5").Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData Source:=Range("test!$A$1:$F$5")
ActiveSheet.Shapes("Chart 14").ScaleWidth 1.5266666667, msoFalse, _
msoScaleFromTopLeft
ActiveSheet.Shapes("Chart 14").ScaleHeight 1.5902777778, msoFalse, _
msoScaleFromTopLeft
ActiveChart.SetElement (msoElementPrimaryCategoryAxisTitleAdjacentToAxis)
Selection.Caption = "horizontal axis title"
ActiveChart.SetElement (msoElementPrimaryValueAxisTitleRotated)
Selection.Caption = "vertical axis title"
ActiveChart.SetElement (msoElementChartTitleAboveChart)
Selection.Caption = "=test!R8C1"
Selection.Format.TextFrame2.TextRange.Font.Size = 14
ActiveChart.ChartArea.Select
ActiveSheet.Shapes("Chart 14").Line.Visible = msoFalse
End Sub
I'm running into a couple problems with this.
One is that it refers specifically to sheet "test", but I need it to apply to whatever sheet I happen to be looking at. That goes for the caption title as well.
The other thing is that it refers to "Chart 14" (you can tell that I've been trying this multiple times, creating and deleting charts as I go along). When I try running it on a different sheet, I get an error message that the item with the specified name wasn't found, and debug shows that it's referring to the chart name.
How do I generalize the macro above so that it's not tied to a specific sheet name or chart number?
Thanks much for any pointers.
Bookmarks