4

I want to change font size on axis xlCategory. I am running the code like below but it is failing. I am stuck in finding workarounds. Chart is "Funnel" type 123. On the other types of chart code is running fine.

With ActiveChart.Axes(xlCategory).TickLabels.font
    .size = 12
    .Name = "Arial"
End With

I get the error:

"object doesn't support this action"

I was also trying to run the othe code but also without any success.

ActiveChart.Axes(xlCategory).Select
With Selection.Format.TextFrame2.TextRange.font
    .size = 12
End With

I get the error:

"method textframe2 of object chartformat failed"

braX
  • 11,506
  • 5
  • 20
  • 33

1 Answers1

1

This is a very peculiar bug!

I guess it has to do with the fact that the XlCategory axis becomes vertical when you are using the "Funnel" chart type:

enter image description here

I've tried all the standard VBA ways I could think of and this doesn't seem to work. I even tried to see if we could save a funnel chart as a template and then use VBA to apply this template, but "Save as Template" is not available for some reason...

enter image description here

So, the reason why I'm explaining all this instead of writing a solution is because there is a workaround, but this kind of thing should only be used as a last resort. And if you haven't guessed it, I'm talking about using the SendKeys method.

Disclaimer: As always, using SendKeys is somewhat tricky and can lead to unexpected results. I would recommend reading some articles to get yourself familiar with it before using it. This article would be a good starting point.

Furthermore, to avoid any issues, don't run a macro that has the SendKeys method using the Run button enter image description here in the VBE since the keystroke must be sent to the Excel window and not the VBE window. I suggest launching the macro using a shortcut to launch your macro. For example, you could use the Application.OnKey method by runnning a macro similar to this:

Sub SetShortcut()

    'Set the shortcut for the macro to be CTRL+ALT+m
    Application.OnKey "^%m", "NameOfYourMacro"

End Sub

An example using Sendkeys

The idea is to use Sendkeys to send the sequence of keystrokes that would give us the result we want. Here, we want to change the size and the font name of the XlCategory axis. By using the correct Alt key shortcut sequence we can achieve this. I'm not sure if the sequence is the same for all versions of Excel, but with Office 365, if we wanted to change the font name, it would be:

  1. Type Alt - to initiate the Alt Key shortcut sequence.
  2. Type "H" - to get the Home tab
  3. Type "FF" - to select the Font Name text box
  4. Type "Arial" and then press Enter - to change the font to Arial.

For this to apply to the XlCategory axis, we need to make sure that it is already selected before we run the macro. If it is selected, the following code should do the trick:

Sub ChangeCategoryAxisFont()

    'Font Name
    Application.SendKeys Keys:="%", Wait:=True
    Application.SendKeys Keys:="H", Wait:=True
    Application.SendKeys Keys:="FF", Wait:=True
    Application.SendKeys Keys:="Arial", Wait:=True
    Application.SendKeys Keys:="~", Wait:=True 'Press Enter

    'Size
    Application.SendKeys Keys:="%", Wait:=True
    Application.SendKeys Keys:="H", Wait:=True
    Application.SendKeys Keys:="FS", Wait:=True
    Application.SendKeys Keys:="12", Wait:=True
    Application.SendKeys Keys:="~", Wait:=True 'Press Enter

    'Restore Numlock (which gets disabled when your macro runs at least one SendKeys command)
    Application.SendKeys Keys:="{NUMLOCK}", Wait:=True

End Sub

If it is not already selected (even if the chart is selected), you might have another problem: if you include ActiveChart.Axes(xlCategory).Select in your macro, the options available in the ribbon won't be updated correctly and the macro above won't work. To let the ribbon update, we would need to end the execution of any macros for a brief second and proceed with our macro after.

For instance, you could use the Application.OnTime method to schedule the macro to run one second later, like this:

Sub PreSelectAxisAndRunMainMacro()

    ActiveChart.Axes(xlCategory).Select
    Application.OnTime Now + TimeSerial(0, 0, 1), "ChangeCategoryAxisFont"

End Sub

I know what you might think: "All this just to make some font changes?!". Yeah, I know, but at least if you become familiar with how to use the SendKeys method, you'll be able to use this in other contexts that require it...

DecimalTurn
  • 3,243
  • 3
  • 16
  • 36
  • Great thanks. That workaround solved my problem on which I spent a lot of hours. – Maciek Wojciechowski Nov 28 '18 at 08:18
  • @MaciekWojciechowski I'm glad I could help. If this solves your question, please consider accepting it by clicking the check mark. This is not an obligation, but it indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. – DecimalTurn Nov 28 '18 at 23:49