View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
joeu2004[_2_] joeu2004[_2_] is offline
external usenet poster
 
Posts: 829
Default How to retrieve number of visible digits after decimal point in VBA

"N Lee" wrote:
I want to retrieve the number of visible decimals
in a cell as an integer.
For instance, a cell has a value of 98.7654321, and
displays as 98.8 because its NumberFormat is
"#,##0.0_);(#,##0.0)". This should return '1'.


Range("A1").Text returns to displayed contents of a cell. So if A1 contains
=PI() with format Number with 2 decimal places, Range("A1").Text returns
"3.14".

If we only had to deal the Number format, the following algorithm would
return the number of decimal places in variable n:

Dim s As String, n As Long
s = Range("a1").Text
n = InStr(s, ".")
If n 0 Then n = Len(s) - n

However, that will not work with your format and negative numbers. The
following is one algorithm (untested), which assumes that you do not have
"." in text. Someone else might provide a regular expression solution,
which would be better.

n = InStr(s, ".")
If n 0 Then
For p = n+1 To Len(s)
If Not IsNumeric(Mid(s, p, 1)) Then Exit For
Next
n = p - n -1
End If