11

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

hackwithharsha
  • 901
  • 1
  • 16
  • 39

1 Answers1

22

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

Stupid_Intern
  • 3,382
  • 8
  • 37
  • 74
  • 10
    Just to clarify a bit further on this - `ThisWorkbook` refers to the workbook in which the code being executed at that point in time resides. This is what I think you're saying anyway in your answer but thought it could be a little clearer. +1 – SierraOscar Feb 16 '16 at 10:36