Link to home
Start Free TrialLog in
Avatar of eshurak
eshurakFlag for United States of America

asked on

Count numbers in string using VB 6.0

Hello,

I need to count the number character in a string using vb 6.0/vba.  For example

NumberCount("%1.2%) = 2

NumbeCount("%8dsf%") = 1

Any ideas?
Avatar of tlovie
tlovie

Maybe this would work:

function NumberCount( s as string ) as long

dim q as string, c as long
q=s
c=0

while c<=9
   q=replace(q, cstr(c),"")
   c=c+1
wend

NumberCount=len(s)-len(q)

end function
Avatar of Patrick Matthews
Add the function below to your project, and use it like this:InputString = "1qw23rt45y6"Debug.Print "There are " & Len(RegExpReplace(InputString, "\D")) & " digits in the string"For more info on the function below, please see my article https://www.experts-exchange.com/Programming/Languages/Visual_Basic/A_1336-Using-Regular-Expressions-in-Visual-Basic-for-Applications-and-Visual-Basic-6.html
Function RegExpReplace(LookIn As String, PatternStr As String, Optional ReplaceWith As String = "", _
    Optional ReplaceAll As Boolean = True, Optional MatchCase As Boolean = True, _
    Optional MultiLine As Boolean = False)
    
    ' Function written by Patrick G. Matthews.  You may use and distribute this code freely,
    ' as long as you properly credit and attribute authorship and the URL of where you
    ' found the code
    
    ' For more info, please see:
    ' http://www.experts-exchange.com/articles/Programming/Languages/Visual_Basic/Using-Regular-Expressions-in-Visual-Basic-for-Applications-and-Visual-Basic-6.html
    
    ' This function relies on the VBScript version of Regular Expressions, and thus some of
    ' the functionality available in Perl and/or .Net may not be available.  The full extent
    ' of what functionality will be available on any given computer is based on which version
    ' of the VBScript runtime is installed on that computer
    
    ' This function uses Regular Expressions to parse a string, and replace parts of the string
    ' matching the specified pattern with another string.  The optional argument ReplaceAll
    ' controls whether all instances of the matched string are replaced (True) or just the first
    ' instance (False)
    
    ' If you need to replace the Nth match, or a range of matches, then use RegExpReplaceRange
    ' instead
    
    ' By default, RegExp is case-sensitive in pattern-matching.  To keep this, omit MatchCase or
    ' set it to True
    
    ' If you use this function from Excel, you may substitute range references for all the arguments
    
    ' Normally as an object variable I would set the RegX variable to Nothing; however, in cases
    ' where a large number of calls to this function are made, making RegX a static variable that
    ' preserves its state in between calls significantly improves performance
    
    Static RegX As Object
    
    If RegX Is Nothing Then Set RegX = CreateObject("VBScript.RegExp")
    With RegX
        .Pattern = PatternStr
        .Global = ReplaceAll
        .IgnoreCase = Not MatchCase
        .MultiLine = MultiLine
    End With
    
    RegExpReplace = RegX.Replace(LookIn, ReplaceWith)
    
End Function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of fabriciofonseca
fabriciofonseca
Flag of Brazil image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of eshurak

ASKER

Thanks Guys,

I'm gonna go with my code below, but it's inspired by some of the solutions above.
Function NumberCount(theString As String) As Long
Dim i As Long
    For i = 1 To Len(theString)
        If IsNumeric(Mid(theString, i, 1)) Then NumberCount = NumberCount + 1
    Next i
End Function

Open in new window

Just out of curiosity what happens with numbers with more than one digit?

Or is it actually digits you want to count?
Avatar of eshurak

ASKER

Imnorie, it's the actual number of digits.