如何调用已经分配给button的vba中的函数

我试图在Excel中创build一个function区,并且我成功了。 现在我已经给一个button分配了一个macros。

Function delete_cells(control As IRibbonControl) 

现在,我创build了另一个macros,我需要从中调用函数delete_cells 。 我试图把它称为如下。

 Function modify_cells(control As IRibbonControl) delete_cells End Sub 

我收到一个错误,说参数不是可选的 。 请帮助我这个错误。

我build议你创build一个单独的子程序,你可以从button的OnAction和其他任何你想要调用它的地方调用,例如:

 'button macro Sub cmdDeleteCells_OnAction(control as iRibbonControl) DeleteCells End Sub 'another Sub that calls the delete routine Sub SomeOtherSub DeleteCells End Sub 'the one they're all talking about Sub DeleteCells msgbox "All your cells are toast, bwah hah hah ha!" End Sub 

编辑:如果你真的想调用button的OnAction子,你也需要传递一个iRibbonControl对象,所以声明一个假的:

 Sub CallTheButtonsCode() Dim FakeControl As IRibbonControl cmdDeleteCells_OnAction FakeControl End Sub 

我真的不推荐这个代码维护的原因,但它的工作原理。

在你的Function delete_cells(control As IRibbonControl)你有一个REQUIRED参数...(control As IRibbonControl) 。 当你调用这个函数时,它应该看起来像这样:

 Function modify_cells(control As IRibbonControl) delete_cells(myControl) 'myControl is your variable. Define what control you want to pass to the function. End Sub