Search code examples
excelvbaworksheet-function

Application.WorksheetFunction vs. WorksheetFunction


This one is a rather short question and probably easy to answer, however I fail to do so myself at this point:


Sample data:

A
B
C

Sample code:

With Sheet1
    Debug.Print Application.WorksheetFunction.Match("D", .Columns(1), 0)    'Option1
    Debug.Print Application.Match("D", .Columns(1), 0)                      'Option2
    Debug.Print WorksheetFunction.Match("D", .Columns(1), 0)                'Option3
End With

Question:

I know that option2 lost intellisense and will not go into debug mode, however option1 and option3 behave the same

  • Intellisense works
  • Error is thrown and code goes into debug-mode

Whereas documentation on the WorksheetFunction object says that we can use the WorksheetFunction property of the Application object, it seems to work just fine without doing so.

So, what is the added value to use Application object reference in this regard and what is the disadvantage of leaving it out?


Solution

  • I'd say that Application is global context and when we use anything, that compiler can't find in its current context, it looks it in Application, eventually finding Application.WorksheetFunction in your case. So both should be equivalent. (this is how it works in JavaScript) BUT I might be wrong.

    UPDATE

    Documentation states, that some functions and properties can be called without Application., so it is true that Application.WorksheetFunction is equivalent to WorksheetFunction, but it is false, that Application serves as global context.

    UPDATE

    According to this interesing article, Application is default object indeed:

    The Application object is the Default Object, Excel assumes it even when it is not specified.