VBA to change Chart fonts on all charts in a sheet

FinUser

New Member
Joined
Aug 10, 2023
Messages
10
Office Version
  1. 2019
Platform
  1. Windows
Hi....I have a macro that loops through all the charts on a sheet updating the font and font size on the chart elements. However, I link each chart titles to specific cells so they are dynamically updated. When I use the macro however it hard codes the link. Any idea how I could tweak the code (below) to stop that happening? Many thanks

Sub ChangeFont()



Dim chrtObj As chartObject

Dim sr As series



For Each chrtObj In ActiveSheet.ChartObjects

With chrtObj.Chart



If .HasTitle Then

.ChartTitle.Format.TextFrame2.TextRange.Font.Name = "Arial"

.ChartTitle.Format.TextFrame2.TextRange.Font.Size = 9

End If



' Change font for axis tick labels if present

If .HasAxis(xlCategory, xlPrimary) Then

.Axes(xlCategory, xlPrimary).TickLabels.Font.Name = "Arial"

.Axes(xlCategory, xlPrimary).TickLabels.Font.Size = 9

End If



' Change font for Secondary axis tick labels if present

If .HasAxis(xlCategory, xlSecondary) Then

.Axes(xlCategory, xlSecondary).TickLabels.Font.Name = "Arial"

.Axes(xlCategory, xlSecondary).TickLabels.Font.Size = 9

End If

If .HasAxis(xlValue, xlPrimary) Then

.Axes(xlValue, xlPrimary).TickLabels.Font.Name = "Arial"

.Axes(xlValue, xlPrimary).TickLabels.Font.Size = 9

End If



' Change font for Secondary axis tick labels if present

If .HasAxis(xlValue, xlSecondary) Then

.Axes(xlValue, xlSecondary).TickLabels.Font.Name = "Arial"

.Axes(xlValue, xlSecondary).TickLabels.Font.Size = 9

End If





' Change font for legend if present

If .HasLegend Then

.Legend.Font.Name = "Arial"

.Legend.Font.Size = 9

End If

End With

Next chrtObj





End Sub
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Try the following...

VBA Code:
If .HasTitle Then

    Dim formula As String
    formula = .ChartTitle.formula

    .ChartTitle.Format.TextFrame2.TextRange.Font.Name = "Arial"
    
    .ChartTitle.Format.TextFrame2.TextRange.Font.Size = 9
    
    .ChartTitle.formula = formula

End If

Hope this helps!
 
Upvote 1
Solution
Try the following...

VBA Code:
If .HasTitle Then

    Dim formula As String
    formula = .ChartTitle.formula

    .ChartTitle.Format.TextFrame2.TextRange.Font.Name = "Arial"
   
    .ChartTitle.Format.TextFrame2.TextRange.Font.Size = 9
   
    .ChartTitle.formula = formula

End If

Hope this helps!
Hero! Thank you!
 
Upvote 0

Forum statistics

Threads
1,215,076
Messages
6,122,987
Members
449,093
Latest member
Mr Hughes

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