Calling an excel macro from the ribbon

╄→гoц情女王★ 提交于 2019-11-30 18:57:47

问题


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). I had already done it successfully in Excel 2007. I am using Custom UI Editor to build a new ribbon, which also works fine. Everything is packaged in a .xlam add-in and added to 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


回答1:


Roberto,

I believe you need to add this param to your macro "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



来源:https://stackoverflow.com/questions/7107507/calling-an-excel-macro-from-the-ribbon

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!