vba 如何返回调用宏的工作表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42000283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 12:01:13  来源:igfitidea点击:

How to return to the worksheet a macro was called from?

excelvbaexcel-vbaworksheet

提问by Tom

I have a macro that is duplicated on buttons on 3 worksheets which moves between worksheets and at the moment once the macro command is completed I have it set to return to the 3rd sheet every time. Is there a command I can put in the code that would return the view to the sheet that the macro was activated on in the first place rather than a set sheet.

我有一个复制在 3 个工作表上的按钮上的宏,它在工作表之间移动,并且一旦宏命令完成,我将它设置为每次都返回到第三个工作表。是否有我可以在代码中放入的命令,该命令会将视图返回到首先激活宏的工作表而不是设置工作表。

回答by Mafii

Save the active sheet in a variable, then, after your macro finished, select the sheet within the variable again:

将活动工作表保存在变量中,然后在宏完成后,再次选择变量中的工作表:

' at the start of your macro:
Dim sourceSheet as Worksheet
set sourceSheet = ActiveSheet


' at the end of your macro:
Call sourceSheet.Activate