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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With