Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 66
Default Find WorksheetFunction - finding text within text

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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Find WorksheetFunction - finding text within text

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.


--
Regards,
Tom Ogilvy

"Marcotte A" wrote in message
...
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



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Search, find or lookup defined text in text string zzxxcc Excel Worksheet Functions 9 September 6th 07 09:37 PM
Finding text in a cell and returning a value based on that text [email protected] Excel Discussion (Misc queries) 5 January 10th 07 06:01 PM
can you find specific text in a string ignoring any other text chriscp Excel Discussion (Misc queries) 1 September 18th 05 09:54 PM
Finding Specific Text in a Text String Peter Gundrum Excel Worksheet Functions 9 April 10th 05 07:21 PM
Unable to get the Text property of the WorksheetFunction class John[_86_] Excel Programming 5 November 25th 04 01:40 AM


All times are GMT +1. The time now is 02:11 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"