Counting the number of backslashes in a string

  • Thread starter Thread starter John Klaassen
  • Start date Start date
J

John Klaassen

Hi you all,

Can anybody tell me if it is possible to count the number of
backslashes in a string? I need the number to appear in a cell.
So if in B1 is "a\b\c" the formula should return 2 in C1.
Any help is appreciated!

Regards,
John
 
One way (in a macro)

Function SlashCount(MyStr As String)
Dim Slash As Integer
Dim RetVal
Slash = 0
For s = 1 To 256
'If InStr(s, myval, "/") > 0 Then
If Mid(MyStr, s, 1) = "/" Then
Slash = Slash + 1
End If
Next
SlashCount = Slash
End Function

Create this in a module and then in C1 enter =SlashCount(B1)

Glen
 
John Klaassen wrote...
Can anybody tell me if it is possible to count the number of
backslashes in a string? I need the number to appear in a cell.
So if in B1 is "a\b\c" the formula should return 2 in C1.

Don't use VBA for something this simple.

C1:
=LEN(B1)-LEN(SUBSTITUTE(B1,"\",""))

More generally, the number of instances of substring ss found in string
s is given by

=(LEN(s)-LEN(SUBSTITUTE(s,ss,"")))/LEN(ss)
 

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

Back
Top