Any reason to avoid Application.Worksheetfunction?

谁说我不能喝 提交于 2019-12-23 10:28:34

问题


I am writing a macro to iterate over some records and wanted to a for-loop to avoid any chance of infinite while looping like:

For i = 0 to COUNT
  **do stuff with START_CELL.Offset(i,0)
Next

I couldn't remember how to do a count of things from VBA so a search sent me here: Use VBA to Count Non Blank Cells in a Column. One suggestion was

n = Worksheets("Sheet1").Range("A:A").Cells.SpecialCells(xlCellTypeConstants).Count

This seemed over complicated, so I did some more digging and decided I was going to use:

COUNT = Application.WorksheetFunction.Count(COUNT_RANGE)

Another example on that page used Application.WorksheetFunction.CountA(), but still now I am concerned (paranoid) there is a reason I should avoid it. Are there any?

Thanks all.


回答1:


The only show-stopper reason to avoid worksheet functions is if you are worried about backwards compatibility with older versions of Excel. New versions of Excel generally introduce new worksheet functions. If these aren't available in your users' versions of Excel you will run into difficulties.

Other than that, and if you know the functions are available on the version your users are working with, they are generally very efficient and often quicker than the equivalent in VBA, especially if they avoid looping through cells, as mentioned in the comments.



来源:https://stackoverflow.com/questions/18518656/any-reason-to-avoid-application-worksheetfunction

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