Excel VBA count numbers before and after decimal

excelvba

Im trying to count the numbers before and after a decimal place.

For example 1452.13 before would be 4 before and after would be 2.

The value is stored in a string i think i may need to use the len with find but cannot figure it out?

would appreciate any help.

Best Answer

There a loads of ways. eg here

This is quite a fast technique to find digits after the decimal point.

It can be copied and modified to find the places before the decimal point

Function CountDecimalPlaces(aNumber As Double) As Long

    Dim len1 As Long, len2 As Long
    len1 = Len(CStr(aNumber))
    len2 = Len(CStr(Int(aNumber)))
    CountDecimalPlaces = len1 - len2 + CLng(len1 <> len2)

End Function


Function CountInteger(aNumber As Double) As Long

    CountInteger = Len(CStr(Int(aNumber)))

End Function

Neither of the above rely on you decimal character being a "."