如何以编程方式查找所谓的macros

我正在尝试编写一个macros,其中有一部分反应有所不同,具体取决于是直接从工作表中的button调用还是从另一个macros间接调用。 我也希望能够有不同的反应,取决于哪个其他macros调用它

在这种情况下,如果macros由button手动激活,或者由特定的其他macros运行,则会popup一个msgbox让用户知道它已经成功完成,但是如果同一个macros运行另一个macros,那么我想跳过这一步。

有没有办法做到这一点?

你可以通过传递参数给子程序来做到这一点。 这是一个非常基本的例子:

 Sub test(number, displayMessage) MsgBox ("The number is " & number) If displayMessage Then MsgBox ("You ran the B sub") End Sub Sub aSub() Call test(4, False) End Sub Sub bSub() Call test(7, True) End Sub 

尝试运行aSub和bSub。 希望这有助于。

这是一种适用于Shapes / Procedures的方法

 Dim SSub As String Sub Sample() On Error Resume Next SSub = Application.Caller '<~~ Already suggested by @Socii On Error GoTo 0 Debug.Print "This procedure was called by " & SSub End Sub Sub Example() SSub = "Example" '~~> (Also see the link below) Sample End Sub 

如果你想把它带到不同的层次,你也可以阅读这个 。 🙂