VBA - Change graph Y axis number format based on cell content

rub

New Member
Joined
May 5, 2021
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Hi,

I'm looking to change the format of the Y axis numbers in a graph based on a number in a cell on the sheet.

So basically:
If value in cell C3 = 1 then format should be "#.##0,00" (just a number with 2 decimals)
If value in cell C3 = 2 then format should be "mm:ss,00"
If value in cell C3 = 3 then format should be "hh:mm:ss

What I was thinking with VBA was the following:
If Range("C3").Value = 1 Then
ActiveSheet.ChartObjects("Grafiek 8").Selection.TickLabels.NumberFormat = "#.##0,00"
ElseIf Range("C3").Value = 2 Then
ActiveSheet.ChartObjects("Grafiek 8").Selection.TickLabels.NumberFormat = "mm:ss,00"
ElseIf Range("C3").Value = 3 Then
ActiveSheet.ChartObjects("Grafiek 8").Selection.TickLabels.NumberFormat = "uu:mm:ss"

But this doesn't seem to work.

Anyone an idea how to make this work? The input of this graph is changing, so that's why I would like the axis to change also.

Thanks!
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Hi rub. Your syntax is a bit wrong but it seems like your logic should work. HTH. Dave
Code:
ActiveSheet.ChartObjects("Grafiek 8").Chart.Axes(xlValue, xlPrimary).TickLabels.NumberFormat = "uu:mm:ss"
ps. please use code tags
 
  • Like
Reactions: rub
Upvote 0
Solution
Hi rub. Your syntax is a bit wrong but it seems like your logic should work. HTH. Dave
Code:
ActiveSheet.ChartObjects("Grafiek 8").Chart.Axes(xlValue, xlPrimary).TickLabels.NumberFormat = "uu:mm:ss"
ps. please use code tags
Awesome, this works perfect! Thanks a lot for the help.

As you seem to know quite a lot on this topic, would you be able to see if you can help me with the issue I posted earlier as well: Change graph axis with VBA based on cell value that is a formula

Thanks a lot!
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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