首页 > 解决方案 > 如何确定一个宏是否被excel中的另一个宏调用,并相应地执行代码

问题描述

这只是我想做的一个例子。我有一个精心制作的宏,我想做不同的事情,具体取决于它是否被另一个宏调用。

sub Example()
Call MyCode
end sub

sub MyCode()
If Called by Example GoTo SkipNextLine
Do these things
exit sub
SkipNextLine:
Do other things
end sub

标签: excelvba

解决方案


一个简单的方法是使用参数和参数。

Sub Example()
    Call MyCode("Example")
End Sub

Sub Example2()
    Call MyCode("Example2")
End Sub

Sub MyCode(Origin as String)
    Select Case Origin
        Case "Example"
            'Do stuff here
        Case "Example2"
            'Do other stuff here
    End Select
End Sub

推荐阅读