Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,045
Default How to retrieve number of visible digits after decimal point in VBA

On Fri, 13 Jul 2012 16:34:38 -0400, Ron Rosenfeld wrote:

On Fri, 13 Jul 2012 13:15:07 -0700, "joeu2004" wrote:

which assumes that you do not have
"." in text.


Excellent point! And one which I failed to consider in my regex solution.

Fortunately, only requires a change in the regex to take that into account. And while I'm sure there are more efficient regexes for this purpose, this seems to work:

==================================
Option Explicit
Function NumDecDispl(rg As Range) As Variant
Dim v() As Variant, c As Range
Dim re As Object
Const sPat As String = "^.*?\S*\.(\d+).*$"
Dim i As Long
Set re = CreateObject("vbscript.regexp")
With re
.Global = False
.Pattern = sPat
.MultiLine = True
End With

If rg.Count = 1 Then
NumDecDispl = Len(re.Replace(rg.Text, "$1"))
Else
ReDim v(0 To rg.Count - 1)
For Each c In rg
v(i) = Len(re.Replace(c.Text, "$1"))
i = i + 1
Next c
NumDecDispl = v
End If
End Function
=====================================


The regex should be changed to:

^.*?\d\.(\d+).*$

and .Global = True
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to check if a number has no more than 2 decimal digits zxcv[_2_] Excel Programming 12 March 25th 10 02:06 PM
Digits to the right of decimal point Tigerxxx Excel Discussion (Misc queries) 3 October 21st 08 09:56 PM
Number format still uses decimal point BecknRoos New Users to Excel 3 April 9th 07 06:32 AM
Converting 2-place decimal value to floating point decimal number with leading zero Kermit Piper Excel Discussion (Misc queries) 3 March 18th 06 06:20 PM
Displaying only digits after the decimal point Tat Excel Worksheet Functions 4 June 1st 05 06:13 PM


All times are GMT +1. The time now is 02:10 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"