Application.WorksheetFunction vs. WorksheetFunction

五迷三道 提交于 2021-02-05 07:35:43

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/59212585/application-worksheetfunction-vs-worksheetfunction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!