简体   繁体   English

更改漏斗图中的轴标签字体大小 (Excel 2016)

[英]Change Axis Label font size in a funnel chart (Excel 2016)

I want to change font size on axis xlCategory .我想更改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.图表是“漏斗”类型 123。其他类型的图表代码运行良好。

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" “对象图表格式的方法 textframe2 失败”

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:我想这与使用“漏斗”图表类型时XlCategory轴变为垂直的事实有关:

在此处输入图片说明

I've tried all the standard VBA ways I could think of and this doesn't seem to work.我已经尝试了所有我能想到的标准 VBA 方法,但这似乎不起作用。 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...我什至尝试查看是否可以将漏斗图另存为模板,然后使用 VBA 应用此模板,但由于某种原因“另存为模板”不可用...

在此处输入图片说明

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.如果您还没有猜到,我说的是使用SendKeys方法。

Disclaimer : As always, using SendKeys is somewhat tricky and can lead to unexpected results.免责声明:与往常一样,使用SendKeys有点棘手,可能会导致意外结果。 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此外,为避免出现任何问题,请勿使用运行按钮运行具有SendKeys方法的宏在此处输入图片说明 in the VBE since the keystroke must be sent to the Excel window and not the VBE window.在 VBE 中,因为击键必须发送到 Excel 窗口而不是 VBE 窗口。 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:例如,您可以通过运行类似于以下内容的宏来使用Application.OnKey方法

Sub SetShortcut()

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

End Sub

An example using Sendkeys使用 Sendkeys 的示例

The idea is to use Sendkeys to send the sequence of keystrokes that would give us the result we want.这个想法是使用 Sendkeys 发送按键序列,这会给我们想要的结果。 Here, we want to change the size and the font name of the XlCategory axis.在这里,我们要更改XlCategory轴的大小和字体名称。 By using the correct Alt key shortcut sequence we can achieve this.通过使用正确的Alt 键快捷键序列,我们可以实现这一点。 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:我不确定所有版本的 Excel 的顺序是否相同,但对于 Office 365,如果我们想更改字体名称,它将是:

  1. Type Alt - to initiate the Alt Key shortcut sequence.键入Alt - 启动 Alt Key 快捷键序列。
  2. Type "H" - to get the Home tab输入“H” - 获取主页选项卡
  3. Type "FF" - to select the Font Name text box输入“FF” - 选择字体名称文本框
  4. Type "Arial" and then press Enter - to change the font to Arial.键入“Arial”,然后按Enter - 将字体更改为 Arial。

For this to apply to the XlCategory axis, we need to make sure that it is already selected before we run the macro .为了将此应用于XlCategory轴,我们需要确保在运行宏之前已选择它。 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.如果尚未选择(即使选择了图表),您可能还会遇到另一个问题:如果您在宏中包含ActiveChart.Axes(xlCategory).Select ,则功能区中的可用选项将无法正确更新,并且上面的宏不起作用。 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:例如,您可以使用Application.OnTime方法安排宏在一秒后运行,如下所示:

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...是的,我知道,但至少如果您熟悉如何使用SendKeys方法,您将能够在需要它的其他上下文中使用它...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM