Bob
Bob

Reputation: 125

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). 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

Upvotes: 3

Views: 9433

Answers (1)

Justin Self
Justin Self

Reputation: 6265

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

Upvotes: 12

Related Questions