简体   繁体   English

从功能区调用excel宏

[英]Calling an excel macro from the ribbon

Intro: I have written some short excel macros (tested, they work fine) and want to link them to a button in the Ribbon (Excel 2010). 简介:我已经编写了一些简短的excel宏(测试过,它们工作正常),并希望将它们链接到功能区中的按钮(Excel 2010)。 I had already done it successfully in Excel 2007. I am using Custom UI Editor to build a new ribbon, which also works fine. 我已经在Excel 2007中成功完成了它。我使用自定义UI编辑器来构建一个新的功能区,它也可以正常工作。 Everything is packaged in a .xlam add-in and added to Excel. 所有内容都打包在.xlam加载项中并添加到Excel中。 The ribbon shows up nicely, all other buttons works, but ... 功能区很好地显示,所有其他按钮都有效,但......

Problem: when I hit the button that is linked to the macro I get the error: "wrong number of parameters or property assignment not valid" (message translated from Italian, might not be exactly the same in English) 问题:当我点击链接到宏的按钮时,我得到错误:“错误的参数数量或属性赋值无效”(从意大利语翻译的消息,英语可能不完全相同)

Troubleshooting info: The macros do not have parameters. 故障排除信息:宏没有参数。 The same macros can be successfully called and executed manually. 可以手动成功调用和执行相同的宏。 I am even able to add the same macros to the Quick Access Toolbar. 我甚至可以将相同的宏添加到快速访问工具栏。

Here is the specific portion of the ribbon script: 以下是功能区脚本的特定部分:

<group id="DupNumber" label="Number" insertBeforeMso="GroupNumber" >  
    <comboBox idMso="NumberFormatGallery"/> 
    <box id="HN1" boxStyle="horizontal"> 
        <buttonGroup id="HNButtonGroup1"> 
            <button id="Euro" onAction="Roberto.xlam!EURZ" imageMso="F" supertip="text ..."/> 
            <button id="EuroNZ" onAction="Roberto.xlam!EURNZ" imageMso="E" supertip="text ..."/> 
            <button idMso="PercentStyle"/> 
            <button id="Comma" onAction="Roberto.xlam!NewCommaFormat" imageMso="C" supertip="test ..."/> 
            <button idMso="PercentStyle"/> 
        </buttonGroup> 
    </box>

and here are the macros: 这是宏:

Sub EURZ()
    Application.ActiveCell.NumberFormat = "€ #,##0.00"
End Sub
Sub EURNZ()
    Application.ActiveCell.NumberFormat = "€ #,##0"
End Sub
Sub NewCommaFormat()
    Application.ActiveCell.NumberFormat = "#,##0"
End Sub

Can you help me? 你能帮助我吗? Thanks Roberto 谢谢罗伯托

Roberto, 罗伯托,

I believe you need to add this param to your macro "control As IRibbonControl" 我相信你需要将这个参数添加到你的宏“control As IRibbonControl”

So it should look like this: 所以看起来应该是这样的:

Sub EURZ(control As IRibbonControl)
    Application.ActiveCell.NumberFormat = "€ #,##0.00"
End Sub

Hope that works for you. 希望对你有用。

-Justin -Justin

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

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