0

Good day.

This may be simple and all, but I would just like to know. Within the first Sub of my Form, I placed at the very beginning of the code the following line:

MsgBox (ActiveWorkbook)

Such line would usually prompt the workbook active (as the call suggests). When placed in other sections of the project, it surely works, but not here. Instead, this error appears:

Run-time error '438':

Object doesn't support this property or method

So, I'd just like to gain more understanding on the subject. Thanks.

PS. The sub I'm placing that line of code in is a simple button_Click().

Thanks again.

Community
  • 1
  • 1
TheBSITGuy
  • 15
  • 5

1 Answers1

1

I have to question the "Such line would usually prompt the workbook active (as the call suggests)" assertion.

MsgBox takes a Variant as an argument, but one that can be successfully cast to a String (MsgBox$ is almost always better practice).

ActiveWorkbook returns a Workbook object, and a Workbook cannot be cast to a String. I don't recall off the top of my head what the default property of Workbook is, but I know it isn't convertible into a String. If you want to get the name of the Workbook, you need this:

MsgBox$ ActiveWorkbook.Name

You would need that anywhere, Form, Class, Module, ...anywhere.

Better practice would be to get a reference to the active Workbook and use that throughout your code. See How to avoid using Select in Excel VBA macros.

Community
  • 1
  • 1
Comintern
  • 21,855
  • 5
  • 33
  • 80