11
votes

Is there any difference between Thisworkbook and 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

2
ThisWorkBook object refers to the workbook that code is in. ActiveWorkBook object refers to the workbook that is currently active.0m3r
Thanks for quick reply Om3rhackwithharsha

2 Answers

21
votes

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

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

E.g if you are writing the code in the module or sheet of workbook A then Thisworkbook.name will return A no matter which is the activeworkbook

-2
votes

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