View Single Post
  #8   Report Post  
Posted to microsoft.public.excel.programming
Ron Rosenfeld[_2_] Ron Rosenfeld[_2_] is offline
external usenet poster
 
Posts: 1,045
Default How to retrieve number of visible digits after decimal point in VBA

On Fri, 13 Jul 2012 13:16:41 -0700 (PDT), N Lee wrote:

Very clever plan. I'll get to trying it out on Monday. Thanks for the in-depth description. I hope it will help me solve this little puzzle.

Nathan


As I mentioned in my reply to joeu2004, who raised the issue of dots within the custom formatting that were not part of the number, I made a change in the regex portion. However, what I posted in response to his is flawed, and the latest iteration of the regex pattern is expressed by:

Const sPat As String = "^.*?\d\.(\d+).*$"

The entire UDF:

============================
Option Explicit
Function NumDecDispl(rg As Range) As Variant
Dim v() As Variant, c As Range
Dim re As Object
Const sPat As String = "^.*?\d\.(\d+).*$"
Dim i As Long
Set re = CreateObject("vbscript.regexp")
With re
.Global = True
.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
============================