Excel:button操作代码 – 为什么我不能使用工作簿和工作表模块

我有一个个人工作地图,包含一个模板(使用vba代码和一个button操作),当需要时我将其复制到工作表中。 它创build一个表格,快速访问许多> 25张,如假popup(excel 2016 macos)。 当我将它直接编程到工作表模块时,它确实工作正常。 它收集表单并用下面的代码创buildbutton。

Set btnRng = TOC_WS.Range(Cells(lastRow, btnCol), Cells(lastRow, btnCol)) Set btn = TOC_WS.Buttons.Add(btnRng.Left, btnRng.Top, btnRng.Width, btnRng.Height) With btn .OnAction = "btnAction" .Caption = WS.Name .Name = WS.Name End With 

和button子….

 Sub btnAction() ...... End Sub 

但是现在由于某种原因,当被调用的子文件在Sheet模块中时,它不起作用。 我收到通知,它无法find。 如果我把它放到一个代码模块的工作。 我当然searchtenetworking,但找不到任何说不能工作的东西。

我的问题 – 我怎样才能让button操作回到与代码的其余部分相同的工作表模块?

一般来说,当例程是工作表的成员时,工作表的名称隐含地是例程名称的一部分。

 btn.OnAction = "Sheet1.btnAction" 

或者,就像在你的代码中,你想把它绑定到TOC_WS表中的一个例程:

 btn.OnAction = TOC_WS.CodeName & ".btnAction" 

而且,如果您想将例程移至代码模块ThisWorkbook

 btn.OnAction = "ThisWorkbook.btnAction"