我尝试在excel中创建一个功能区,但我成功了。现在我已经为一个按钮分配了一个宏。
Function delete_cells(control As IRibbonControl)
现在,我创建了另一个宏,我需要从中调用函数delete_cells
。我试着把它称之为。
Function modify_cells(control As IRibbonControl)
delete_cells
End Sub
我收到错误消息 Argument not optional 。请帮我解决这个错误。
答案 0 :(得分:5)
我建议你创建一个单独的子程序,你可以从按钮的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
编辑:如果你真的只想调用按钮的OnAction子,你需要传递一个iRibbonControl对象,所以声明一个假的:
Sub CallTheButtonsCode()
Dim FakeControl As IRibbonControl
cmdDeleteCells_OnAction FakeControl
End Sub
出于代码维护的原因,我不推荐这样做,但它确实有效。
答案 1 :(得分:0)
在您的Function delete_cells(control As IRibbonControl)
中,您有一个必需的参数...(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