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

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
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,223,885
Messages
6,175,182
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