How to determine visible decimal places

Guentjo

Active Member
Joined
Jul 27, 2004
Messages
351
Office Version
  1. 2021
Platform
  1. Windows
In VBA, how can I determine how many decimal places are visible in the activecell?

let's say the true value of the cell is "1.23", but I have it fomatted to "0.0" so what I see is "1.2"

I'd like the code to return a value of "1" because 1 digit is visible to the right of the decimal point.
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
How about a User Defined Function?

Code:
Function DecimalsShowing(Rng As Range) As Integer
Dim sNum As String, i, iDecimal As Integer
sNum = Format(ActiveCell.Value, ActiveCell.NumberFormat)
DecimalsShowing = 0
iDecimals = 9.9 * 10 ^ 307
For i = 1 To Len(sNum)
  If InStr(1, sNum, ".") Then iDecimal = InStr(1, sNum, ".")
  If i > iDecimal Then DecimalsShowing = i - iDecimal
Next i
End Function

Then in your existing code,

NumberOfDecimalPlaces = DecimalsShowing(ActiveCell)
 
Upvote 0
Jon,
Won't that count all decimal places regardless of whether or not they are displayed? I thougt the OP only wants to know the number of decimal places that are visible?

Yeah, totally mis-read the post ... Bleck! Gotta give up workin', I guess... :-D
 
Upvote 0
Hi, guys,
perhaps I'm missing something here, at the moment Teacher's code is not giving what I would expect, but i'm sure you tested it ...

would this do ?
Code:
Sub test()
nr = ActiveCell.NumberFormat
d = InStr(1, nr, ".")

    If d = 0 Then
    MsgBox "0"
    Else
    MsgBox Len(nr) - d
    End If

End Sub
of course this wouldn't be correct if the numberformat is general or any other non-numeric-format
kind regards,
Erik
 
Upvote 0
I'm experiencing problems with different formats with both options.

Currency (being on of the formats I'm testing) gives very strange results (different) with both options

With the UDF
$4.40 returns a value of 4 digits
$(4.40) returns a value of 3 digits

With e.v.g.'s code
the return for both $4.40 and $(4.40) is 39

Of course the Currency format is truely:
_($* #,##0.00_);_($* (#,##0.00);_($* "-"??_);_(@_)

you will notice there are 39 digits to the right of the first decimal point in the above string.

Odd. Similar problems occur with different formats. for the UDF, if the cell value is "1" and formated as "General" the DecimalsShowing = 7. which I think is from the 7 letters in the word general.
 
Upvote 0
what seemed to be rather simple when reading your initial post, turns out to be more complicated
perhaps a closer look will bring a solution, but first I would like to know: what do you need this for ?
 
Upvote 0
could perhaps be enhanced
anyway this is working in most of the cases for me
Code:
Sub test()
'still not working when formatted as time or fraction or custom
If Not IsNumeric(ActiveCell) Then
MsgBox "not numeric", 48, "ERROR"
Exit Sub
End If

nr = ActiveCell.NumberFormat
d = InStr(1, nr, ".")

    If d = 0 Then
    nr = ActiveCell
        d = InStr(1, nr, ",")   'US "."
        If d = 0 Then
        MsgBox "0"
        Exit Sub
        End If
    End If
    
    cnt = 0
    For n = d + 1 To Len(nr)
        If Mid(nr, n, 1) = "0" Or Mid(nr, n, 1) = "#" Or IsNumeric(Mid(nr, n, 1)) Then
        cnt = cnt + 1
        Else
        Exit For
        End If
    Next n
    MsgBox cnt
End Sub
still curious about your purpose

best regards,
Erik
 
Upvote 0

Forum statistics

Threads
1,222,713
Messages
6,167,813
Members
452,141
Latest member
CaseyJones1979

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top