View Single Post
  #7   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: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
=====================================