Pass to long or cast to int, remember the following.
These functions are one of the presentation functions in Excel VBA, which depend on the regional settings of the system. Therefore, if you use a comma in your double, as in some countries in Europe, you will experience a mistake in the USA.
For example, in the European version, excel version 0.5 will work well with CDbl (), but in the US version this will result in 5. Therefore, I recommend using the following alternative:
Public Function CastLong(var As Variant) ' replace , by . var = Replace(var, ",", ".") Dim l As Long On Error Resume Next l = Round(Val(var)) ' if error occurs, l will be 0 CastLong = l End Function ' similar function for cast-int, you can add minimum and maximum value if you like ' to prevent that value is too high or too low. Public Function CastInt(var As Variant) ' replace , by . var = Replace(var, ",", ".") Dim i As Integer On Error Resume Next i = Round(Val(var)) ' if error occurs, i will be 0 CastInt = i End Function
Of course, you can also recall cases when people use commas and periods, for example, three thousand, like 3000.00. If you need functions for such cases, you need to check out another solution.
PeterH Jun 19 '13 at 12:32 2013-06-19 12:32
source share