Find WorksheetFunction - finding text within text

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a long list of strings. I want to return the first part of each
string, up to the first space or foward slash ("/"). I can't seem to use the
Find function correctly (does it only work for searching through ranges?).

Here is my code - not the most elegant... any ideas?

Function ExtractClient(mystr As String) As String

Dim i As Single
Dim SpacePos As Single
Dim SlashPos As Single
Dim NoSpace As Boolean
Dim NoSlash As Boolean
Dim MinDelim As Integer

NoSpace = False
NoSlash = False

With Application.WorksheetFunction
If .IsError(.Find(" ", mystr, 1)) = True Then
'no space in string
NoSpace = True
Else
SpacePos = .Find(" ", mystr, 1)
End If

If .IsError(.Find("/", mystr, 1)) = True Then
'no slash in string
NoSlash = True
Else
SlashPos = .Find("/", mystr, 1)
End If

If NoSlash = True And NoSpace = True Then
ExtractClient = ""
Else
If NoSlash = True Then
ExtractClient = .Left(mystr, SpacePos - 1)
Else
If NoSpace = True Then
ExtractClient = .Left(mystr, SlashPos - 1)
Else
MinDelim = .Min(SpacePos, SlashPos)
ExtractClient = .Left(mystr, MinDelim - 1)
End If
End If
End If
End With

End Function
 
don't use the worksheetfunction Find. Use the VBA function Instr

If you must go with your current code, remove WorksheetFunction. so just
Application. remains. then, given it is otherwise written correctly, it
will work.
 

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