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.
 
Purpose:

I have to appease someone who doesn't like it when a spreadsheet apears to show 1 + 1 = 3, when it's really 1.3+1.3 = 2.6, but I'm not showing any decimals

to that end, I have created a macro that will change the total line in several templates from the standard "=sum()" to a more complex array "{=SUM(ROUND(),#))}".

See rows 4 & 5
Rounding Totals with macro.xls
ABCD
1Line11.31.3
2Line21.41.4
3Line31.81.8
4Total4.54.4
5RoundedTotal4.54.5
Sheet1


I have the code to change the formulas accordingly, which I'm more than happy to share if you want to see it. I adapted an earlier rounding tool. This works great, if the round to digit ("#" from above) is the same across the board, and I could make it a constant. Unfortunately it is not, so I can't. The purpose of this code is to look at the activecell and change the round to digit in the array to the number of visble digits right of the decimal point. So if the cell(which is the sum of a column) has 2 decimal places (as would all the cells in the column) then the array would round to "2". One digit right of the decimal point and the macro would change the array formula to round to 1.

I guess I'm trying to fake "Percision as Displayed" - sort of.
 
Upvote 0

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Seems like a workaround which I wouldn't like to use. Instead I would adapt the display ... But perhaps I'm missing something oh! my english and the late hour here (surprised it's almost 3AM :oops: )
anyway: did you see my previous post which came with a "solution" ?
 
Upvote 0
Erik brings up a lot of good points. I took a look at my UDF, and realized I left in an "ActiveCell" where I should have had Range. I fixed that, followed Eriks IsNumeric idea so that the code ignores nonumeric characters. It now appears to work with currency.


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

General Format is still a problem, because the number of characters showing depends on the column width, not the number format.....hmm...
 
Upvote 0
Seems like a workaround which I wouldn't like to use. .... did you see my previous post which came with a "solution" ?

Absolutely is a work around, but I thought it was a clever one and much better then the alternative of hardcoding something :evil:

I haven't had a chance to worrk with your most recent post, I saw it and wanted to explain my situation, then something else came up before I could test.

I'll work with it and let you know. I see the Pennslyvanian has responded again as well, so I'll check that out too.
 
Upvote 0
General Format is still a problem, because the number of characters showing depends on the column width, not the number format.....hmm...

I agree. The new UDF seems to work with all number formats except general. It even works with the "Text" format.

e.v.g.'s code has the same limitation with the general format and also doesn't work with text format (note: this is an observation only and certainly isn't a requirement for my needs).

I've also seen that both work with limited (at least) custom formats.

I doubt that I'll ever run into cells that have been formatted as 'general' so hopefully that problem isn't really one :) .

I think I'll go with one of these. Thanks for your help. Very clever solutions.
 
Upvote 0
e.v.g.'s code has the same limitation with the general format and also doesn't work with text format (note: this is an observation only and certainly isn't a requirement for my needs).

Check that.

I noticed this note in the code:
Code:
d = InStr(1, nr, ",")   'US "."

so I changed the comma to a period b/c I'm in the States.

wah la (sp?) both general and text now work :-P
 
Upvote 0
Yep! it's the details which make the difference !!

curious:
I know this
wah la = voilà :-)
but what is (sp?) ?

EDIT: my eldest son just said sp = spelling
 
Upvote 0
You Americans never need French :lol:
my place is at 30 kilometers from Wallonie: part of Belgium with French-speaking-people
at 100 km from Germany
at 200 km from England
so we can use some language-knowledge here :-)
 
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