Count Nos in string

B

billypit786

Hi,
I have string like below and i wanna count nos in that string

1st string ;0;80;0;0;39;0;0;0;81;42;5;100;0;0 in this string there are
6 Nos.

2nd string ;11;0;5;2;0;0;0;0;89;65;0;26;0;2;5 in this line there are 8
Nos.

3rd string ;0;0;0;0;50;41;20;30;0;50;0 in this line there are 5 Nos.

I can count total nos in string

1st string -14 Nos.
2nd string -15 Nos.
3rd String - 11 Nos.

can anyone help please?
Thank You.
 
J

Jeff Boyce

One approach might be to create a procedure that steps through the string,
one "number" at a time, checking to see if it is zero or non-zero. If the
value is non-zero, add 1 to a counter of non-zero values. Regardless, add
one to the "total values" counter.

You could use a For... Next loop and the Len() function to inspect each
character.

Good luck!

Regards

Jeff Boyce
Microsoft Office/Access MVP
 
N

NKTower

Put the following ( between the dashed lines) in a MODULE.
I have wrapped lines using the sequence <space>underline so that it should
fit in the board posting. Unwrap the lines if you like.
----------------------------------
Public Function Load_Array_from_String( _
str_Input As String, _
str_Delimiter As String, _
strary_Output() As String) _
As Integer
Dim I As Integer
Dim I_Last As Integer
Dim N As Integer
Dim str_TempIn As String
Dim L_TempIn As Integer
Dim L_Delim As Integer
Dim Found_At As Integer


str_TempIn = Trim(str_Input)
L_TempIn = Len(str_TempIn)
L_Delim = Len(str_Delimiter)
I_Last = L_TempIn - L_Delim + 1

For I = 1 To I_Last
If Mid$(str_TempIn, I, L_Delim) = _
str_Delimiter Then N = N + 1
Next I
N = N + 1 ' There will always be at least one output
ReDim strary_Output(N)

I = 0
Found_At = _
InStr(str_TempIn, str_Delimiter)
While Found_At > 0
If Found_At = 1 Then
I = I + 1
strary_Output(I) = ""
Else
I = I + 1
strary_Output(I) = _
Mid$(str_TempIn, 1, Found_At - 1)
End If
If Found_At + L_Delim <= L_TempIn Then
str_TempIn = Mid$(str_TempIn, _
Found_At + L_Delim)
L_TempIn = Len(str_TempIn)
Else
str_TempIn = ""
L_TempIn = 0
GoTo Done
End If
Found_At = InStr(str_TempIn, _
str_Delimiter)
Wend
Done:
If Len(str_TempIn) > 0 Then
I = I + 1
strary_Output(I) = str_TempIn
End If
Load_Array_from_String = N
End Function

Public Function CountNumbers(strIn As String, D As String) As Integer
Dim I as Integer
Dim N As Integer
Dim C As Integer
Dim strResult() As String

C = 0
N = Load_Array_from_String(strIn,D,strResult)
For I = 1 to N
If IsNumeric(strResult(I) Then
C = C + 1
End If
Next I
CountNumbers = C
End Function
-----------------------------


N = CountNumbers("X;0;5;10;30;Y;45",";")

N will have value of 5, it will skip over the X and Y
 
K

Klatuu

Function NumberCount(strList As String) As Long
Dim varNumbers As Variant
Dim lngX As Long

varNumbers = Split(strList, ";")
For lngX = 0 To Uboud(varNumbers)
If varNumbers(lngx) <> 0 Then
NumberCount = NumberCount + 1
End If
Next LngX
End Function
 
K

Klatuu

A small modification to my previous post. I just noticed the string may
start with the ;. That would mean the first element of the array (0) would
then be = vbNullString and would count as a valid number.

Sorry about the oversight, You need to test for that condition as well so
you don't count it as a number.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

selecting nos 2
how to solve 1
help please 1
find balance nos 3
selecting nos from table 1
Conditional Counting 1
arranging in particular order 1
help to format report 1

Top