简体   繁体   English

如何从自定义功能区中的按钮运行宏?

[英]How to run a macro from button in custom ribbon ?

I created a custom ribbon using Custom UI Editor For Microsoft Office 我使用Microsoft Office的自定义UI编辑器创建了一个自定义功能区

<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui">
  <ribbon>
    <tabs>
      <tab idMso="TabHome" >
        <group id="customGroup1" label="My Group" insertAfterMso="GroupEditingExcel">
          <button id="customButton1" label="Click Me" size="large" onAction="test()" imageMso="HappyFace" />
        </group>
      </tab>
    </tabs>
  </ribbon>
</customUI>

I saved excel 2007 file as Excel Macro-Enabled workbook 我将Excel 2007文件保存为Excel Macro-Enabled工作簿

Here is my macro. 这是我的宏。 It works fine when it run manually 手动运行时效果很好

Sub test()
For i = 1 To 10
    For j = 1 To i
    Cells(i, j) = j
    Next
Next
End Sub

But When I press the smiling face button in my custom ribbon it gives following error message. 但是,当我按自定义功能区中的笑脸按钮时,会显示以下错误消息。

cannot run the macro 'test()' the macro may not be available in this workbook or all macro may be disabled. 无法运行宏“ test()”,该宏可能在此工作簿中不可用,或者所有宏都可能被禁用。

How can I fix this issue ? 如何解决此问题? Any suggestions ? 有什么建议么 ?

Thank you 谢谢

You should not include the brackets. 您不应该包括方括号。 So change this: 所以改变这个:

onAction="test()" onAction =“ test()”

to: 至:

onAction="test" onAction =“ test”

Also, make sure the routine name is unique and does not exist in any other workbook you might have open in Excel, otherwise you risk the wrong routine being called. 另外,请确保例程名称是唯一的,并且在您可能已经在Excel中打开的任何其他工作簿中均不存在该例程名称,否则您将面临调用错误例程的风险。

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

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