Excel VBA secondary axis title

Gitad

New Member
Joined
Jun 13, 2024
Messages
5
Office Version
  1. Prefer Not To Say
Platform
  1. Windows
Hello, I am trying to plot 4 curves in the same chart; two on primary axis and two on secondary axis using VBA. The plots are done okay. But when I try to write the secondary axis title it gives Runtime error/automation error. It does not like setting the title (right now those 5 lines are commented) for secondary axis.
Any help/lead would be appreciate.d

With Worksheets("Graph").Shapes.AddChart.Chart
.Parent.Left = Range("B6").Left
.Parent.Top = Range("B6").Top
.Parent.Height = 300
.Parent.Width = 500
.Legend.Position = xlLegendPositionBottom

'remove existing series, if any
While .SeriesCollection.Count > 0
.SeriesCollection(1).Delete
Wend

'set properties for chart
.HasTitle = True
.ChartType = xlLine
.HasLegend = True

.HasAxis(xlValue, xlPrimary) = True
With .Axes(xlValue, xlPrimary)
.HasTitle = True
.AxisTitle.Caption = "Watt"
End With

'.HasAxis(xlValue, xlSecondary) = True
'With .Axes(xlValue, xlSecondary)
'.HasTitle = True
'.AxisTitle.Text = "Secondary Y Axis"
'End With

.ChartTitle.Text = "Demo Plot"
.Axes(xlCategory).CategoryType = xlCategoryScale
.Axes(xlCategory).TickLabels.Orientation = 45 'x-labels in 45deg
.ChartArea.Format.Fill.ForeColor.RGB = RGB(230, 255, 255)

'add each series to chart
Dim j As Long
For j = 2 To 5 Step 1
Debug.Print j
With .SeriesCollection.NewSeries
If j Mod 2 = 0 Then
.AxisGroup = 2
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Hi Gitad. HTH. Dave
VBA Code:
.AxisTitle.Characters.Text = "Secondary Y Axis"
ps. please use code tags
 
Upvote 0
Hi Gitad. HTH. Dave
VBA Code:
.AxisTitle.Characters.Text = "Secondary Y Axis"
ps. please use code tags
Hi Dave, Thank you so much for responding. Unfortunately, that error is still there. And, yes I should have used code tags.
 
Upvote 0
Hmmm... maybe you need to be more specific. You may need to name the chart. This works for me. Dave
VBA Code:
ChtCnt = Sheets("Graph").ChartObjects.Count
Sheets("Graph").ChartObjects(ChtCnt).Name = "ChartName"
With Sheets("Graph").ChartObjects("ChartName").Chart
.HasAxis(xlCategory, xlSecondary) = True
.Axes(xlValue, xlSecondary).HasTitle = True
.Axes(xlValue, xlSecondary).AxisTitle.Characters.Text = "Secondary Y Axis"
End With
 
Upvote 0

Forum statistics

Threads
1,223,880
Messages
6,175,154
Members
452,615
Latest member
bogeys2birdies

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