XcvrTester
New Member
- Joined
- May 17, 2017
- Messages
- 1
I am trying to create a very simple Excel macro in Office 2013. The macro should create a line chart with titles on the x and y axes. I go to the "Developer" tab and select "Record Macro". I create my chart and everything looks fine. When I view my auto-generated VBA code it looks like the following:
The problem is that when I try to run this macro, the y-axis title does not get created and instead the x-axis has the title intended for the y-axis. If I go into the VBA code directly, comment out the line "Selection.Caption = "This is my y axis title"" and manually replace it with the code as shown below everything works fine.
My questions is.....Why does the "auto-generated" VBA code not work correctly? I am using Microsoft Office Professional Plus 2013.
Thanks!
Code:
Sub SimplePlotExample()
Range("A1:B11").Select
ActiveSheet.Shapes.AddChart2(332, xlLineMarkers).Select
ActiveChart.SetSourceData Source:=Range("Sheet1!$A$1:$B$11")
ActiveChart.SetElement (msoElementPrimaryCategoryAxisTitleAdjacentToAxis)
Selection.Caption = "This is my rows title"
ActiveChart.SetElement (msoElementPrimaryValueAxisTitleAdjacentToAxis)
Selection.Caption = "This is my y axis title"
Range("A1").Select
End Sub
The problem is that when I try to run this macro, the y-axis title does not get created and instead the x-axis has the title intended for the y-axis. If I go into the VBA code directly, comment out the line "Selection.Caption = "This is my y axis title"" and manually replace it with the code as shown below everything works fine.
Code:
Sub SimplePlotExample()
Range("A1:B11").Select
ActiveSheet.Shapes.AddChart2(332, xlLineMarkers).Select
ActiveChart.SetSourceData Source:=Range("Sheet1!$A$1:$B$11")
ActiveChart.SetElement (msoElementPrimaryCategoryAxisTitleAdjacentToAxis)
Selection.Caption = "This is my rows title"
ActiveChart.SetElement (msoElementPrimaryValueAxisTitleAdjacentToAxis)
' Add the 2 lines of code below manually to my macro code
ActiveChart.Axes(xlValue, xlPrimary).HasTitle = True
ActiveChart.Axes(xlValue, xlPrimary).AxisTitle.Text = "This is my hand-edited y axis title"
' Selection.Caption = "This is my y axis title" ### This is the auto-generated line commented out
Range("A1").Select
End Sub
My questions is.....Why does the "auto-generated" VBA code not work correctly? I am using Microsoft Office Professional Plus 2013.
Thanks!