Excel 2013 auto generated macro VBA code does not work when attempting to run the macro

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:

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!
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

Forum statistics

Threads
1,224,823
Messages
6,181,169
Members
453,021
Latest member
Justyna P

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