vba VBA中Thisworkbook.name和Activeworkbook.name的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35426907/
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-08 10:13:35  来源:igfitidea点击:

Difference between Thisworkbook.name and Activeworkbook.name in VBA

excelvbaexcel-vba

提问by Harsha Vardhan

Is there any difference between Thisworkbook and ActiveWorkbook.

Thisworkbook 和 ActiveWorkbook 之间有什么区别吗?

Example code :

示例代码:

  Sub workbook_name()
     MsgBox Thisworkbook.name
  End Sub



 Sub active_name()
     MsgBox Activeworkbook.name
  End Sub

Both will return the same output,

两者都将返回相同的输出,

Is there any other instances where we have to use particularly ThisWorkbook where ActiveWorkbook doesn't work

是否有任何其他实例我们必须特别使用 ThisWorkbook ,而 ActiveWorkbook 不起作用

回答by Stupid_Intern

Activeworkbook.nameis used to get the name of the active workbook from n different number of opened workbooks.

Activeworkbook.name用于从 n 个不同数量的打开工作簿中获取活动工作簿的名称。

Thisworkbook.nameis used to get the name of the workbook in which the code is written or stored in the module of that workbook.

Thisworkbook.name用于获取在该工作簿的模块中编写或存储代码的工作簿的名称。

E.g if you are writing the code in the module or sheet of workbook Athen Thisworkbook.namewill return Ano matter which is the activeworkbook

例如,如果您在模块或工作簿表中编写代码,A则无论哪个是活动工作簿Thisworkbook.name都会返回A