VBA question - searching for one of several characters in a string

CosmoPants

New Member
Joined
Mar 9, 2011
Messages
7
Afternoon,

Long time lurker, first time poster - I'm trying to do something that I'm sure ought to be quite simple, but I'm absolutely stuck.

I'm reasonably sure I'm being thick, actually. Apologies if this is a tediously stupid question:

I'm pretty used to writing Crystal Reports, but have recently started to write some VBA too. Essentially, I'm trying to get Excel to loop through a string and count the occurence of certain characters. There are several characters which, if encountered by the loop, should ping the counter. My code looks something like this:

Function PRESENT(range As String)
Application.Volatile

Dim rangelen As Long
rangelen = Len(range)
chars = Array("/", "\", "W")

Dim i As Long
Dim n As Long


For i = 1 To rangelen
If Mid(range, i, 1) = "/" Then
n = n + 1
End If
Next i

PRESENT = n
End Function


which just counts the character "/" of course - I want it to count the characters in the array "chars." I can't find a way of doing it - I want to do something like:

For i = 1 To rangelen
If Mid(range, i, 1) in chars Then
n = n + 1
End If
Next i

or

For i = 1 To rangelen
If Mid(range, i, 1) = {"/", "\", "W"} then
n = n + 1
End If
Next i

but I can't find any referece to the right syntax. My other thought was to have a second loop to change the character being searched for, but it seems like a faff.

Any thoughts? Thanks muchly!
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Try

Code:
Function PRESENT(range As String)
Application.Volatile

Dim rangelen As Long
rangelen = Len(range)
chars = Array("/", "\", "W")

Dim i As Long
Dim n As Long
Dim j As Long

For i = 1 To rangelen
    For j = LBound(chars) To UBound(chars)
        If Mid(range, i, 1) = chars(j) Then
            n = n + 1
        End If
    Next j
Next i

PRESENT = n
End Function
 
Upvote 0
Actually (more efficient)

Rich (BB code):
Function PRESENT(range As String)
Application.Volatile

Dim rangelen As Long
rangelen = Len(range)
chars = Array("/", "\", "W")

Dim i As Long
Dim n As Long
Dim j As Long

For i = 1 To rangelen
    For j = LBound(chars) To UBound(chars)
        If Mid(range, i, 1) = chars(j) Then
            n = n + 1
            Exit For
        End If
    Next j
Next i

PRESENT = n
End Function
 
Upvote 0
Here's an alternative:

Code:
Function Present(ByVal strVal As String, strMtch As String) As Long
    Dim lngLength As Long
    Dim i As Long
    
    
    
    lngLength = Len(strVal)
    
    For i = 1 To Len(strMtch)
        strVal = Replace(strVal, Mid(strMtch, i, 1), "")
    Next i
    
    Present = lngLength - Len(strVal)
    
    
End Function

Use like:

Code:
MsgBox Present(myString,"\/W")
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,740
Members
452,940
Latest member
Lawrenceiow

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top