Adding in Axis Titles using VBA

Kawule

New Member
Joined
Nov 21, 2016
Messages
23
I'm trying to figure out how to add in Axis titles in my code below
Code:
Sub CreateChart()Dim rng As Range
Dim cht As ChartObject
Dim ws As Worksheet, ws2 As Worksheet


Set ws = Worksheets("Daily Data Transfer")
Set ws2 = Worksheets("Daily Report")
Set rng = ws.Range("B1:C31,G1:G31,Q1:R31")
Set cht = ws2.ChartObjects.Add(Left:=50, Width:=283.5, Top:=50, Height:=170)
With cht.Chart
    .HasTitle = True
    .ChartTitle.Text = "pH/Temp°C vs Dosage mg/L"
    .SetSourceData Source:=rng
    .ChartType = xlLine
    .SetElement (msoElementPrimaryValueAxisShow)
    .SetElement (msoElementPrimaryValueAxisTitleAdjacentToAxis)
End With




End Sub

Most of this was trial and error via Macro Recorder but I'm stumped after the .SetElement part as I don't think there is an easy way to do it when I'm using Chart as a variable type? I know in the Macro recorder it will do something like this:
Code:
ActiveChart.Axes(xlValue, xlPrimary).AxisTitle.Text = "pH"
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try this:

Code:
'X axis name
.Axes(xlCategory, xlPrimary).HasTitle = True 
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "X-Axis" 
'y-axis name
.Axes(xlValue, xlPrimary).HasTitle = True 
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Y-Axis"
 
Upvote 0
Well that worked! I wonder why the Macro Recorder version doesn't work unless its using the Charts2 object in VBA?

My next step is to plot the data onto a secondary axis is that also done with .setelement?
 
Upvote 0
The macro recorder does its own thing, no idea.

Secondary axis should be
Code:
.HasAxis(xlCategory, xlSecondary) = True
.Axes(xlCategory, xlSecondary).MinimumScale = Range("A").Value
.Axes(xlCategory, xlSecondary).MaximumScale = Range("B").Value
unless my memory fails me.
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,692
Members
448,979
Latest member
DET4492

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