count-if

L

Laura

hi!
i'm a novel programmer in vba and i am looking for a funtion or procedure
that returns the number of times that a specific character appears in a
given paragraph

I´ll put an example to explain myself better:

dim text as string
dim count as integer

text= "name * surname * age "
' XXXXX is my desired procedure:
count= XXXXX(text, *)


' this procedure must return '2', that is number of asterisks


anybody knows if there is any vba funtion to make the count? or some idea to
construct my procedure?
 
G

Greg Maxey

Perhaps something like this:

Sub ScratchMacro()
Dim myString As String
Dim myChar As String
myString = "name * surname * age"
myChar = Asc("*")
MsgBox Count(myString, myChar)
End Sub
Function Count(pString, pChar) As Long
Dim i As Long
For i = 1 To Len(pString)
If Asc(Mid(pString, i, 1)) = pChar Then
Count = Count + 1
End If
Next
End Function
 
L

Laura

thanks a lot to both!
Jezebel: i'm very impress with your idea: is simple and brilliant, is just
what i was looking for
 

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

Top