Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any reason to avoid Application.Worksheetfunction?

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.

like image 952
ptpaterson Avatar asked Aug 29 '13 18:08

ptpaterson


1 Answers

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.

like image 56
Jamie Bull Avatar answered Oct 14 '22 16:10

Jamie Bull