Solved

Count numbers in string using VB 6.0

Posted on 2010-09-17
7
697 Views
Last Modified: 2013-11-25
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?
0
Comment
Question by:eshurak
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 7

Expert Comment

by:tlovie
ID: 33703929
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
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 33703936
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 http://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

0
 
LVL 8

Accepted Solution

by:
fabriciofonseca earned 250 total points
ID: 33704027

Public Function NumberCount(ByVal Str As String) As Integer

    Dim Numbers As String
    Dim i As Integer
    Dim TotalNumbers As Integer
   
    Numbers = "0123456789"
    TotalNumbers = 0
    i = 1
   
    While i <= Len(Str)
        If InStr(Numbers, Mid(Str, i, 1)) > 0 Then TotalNumbers = TotalNumbers + 1
        i = i + 1
    Wend
    NumberCount = TotalNumbers

End Function
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Assisted Solution

by:VBClassicGuy
VBClassicGuy earned 250 total points
ID: 33704064
Or use a simple old fashioned way:
Function NumberCount (txt As String) As Long
   Dim i As Long, c As Integer, cnt As Long
   For i = 1 to len(txt)
      c = Asc(Mid$(txt,i,1)
      If c > 47 And c < 58 Then
         cnt = cnt + 1
      End If
   Next i
   NumberCount = cnt
End Function
0
 
LVL 3

Author Comment

by:eshurak
ID: 33704321
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

0
 
LVL 34

Expert Comment

by:Norie
ID: 33709420
Just out of curiosity what happens with numbers with more than one digit?

Or is it actually digits you want to count?
0
 
LVL 3

Author Comment

by:eshurak
ID: 33720687
Imnorie, it's the actual number of digits.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Spell Check in VB6 13 152
VB 6 error 5 in windows 10 but not in XP 7 72
backup program with robocopy 6 92
Remove Exif Orientation before adding image to db 9 63
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question