You can’t count on money

An interesting bug is reported at Daily Dose of Excel:
Code snipit to test

If you use a variable of data type currency as a counter in VBA you get some very strange behaviour.  The function below returns the result of counting between the specified limits:
Function CheckCurrency(startval, endval)
Dim c As Currency

For c = startval To endval
Next c

CheckCurrency = c

End Function

The function should return a value one greater than “endval”, but the screen shot below shows the results of using currency as a counter:

currency

Just to prove it is the currency data type causing the problem, here are the results of the same function, with the counter changed to a long:

currency1

The message is, don’t count on currency.

This entry was posted in Excel, VBA. Bookmark the permalink.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.