View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
joel joel is offline
external usenet poster
 
Posts: 9,101
Default Best/Easiest way to search/find in a string

You have to consider if the CASE makes a difference in you text. You may
want to force all text to uppercase before you do a comparison. Instr is
case sensitive, while using "=" is not case sensitive.

"Rick Rothstein (MVP - VB)" wrote:

I know a few ways to search or find terms in a string, but what method
is the best/quickest/easiest?

For example, if I have a string and want to find a different string
within that.

I want to see if the string:

"TEMP(302)WPRDRAMT|input_range = "0" "999999.99" ; |MaxInput = 10|"

Contains the string:

"input_range"

And if so I want to extract the terms "0" and "999999.99".


Part of the decision rests on the EXACT format of your Source string... if
input_range is in the Source string, is it always followed by space/equal
sign/space, are there always exactly two values following it, are the values
always surrounded by quote marks, do you want the quote marks in your
result, etc., etc.? Here is an approach given that the Source String we see
is what you actually have...

Dim Source As String
Dim LowerRange As String
Dim UpperRange As String
Dim Fields() As String
Source = """TEMP(302)WPRDRAMT|input_range = ""0"" ""999999.99"" ; |MaxInput
= 10|"""
If InStr(1, Source, "input_range", vbTextCompare) Then
Fields = Split(Trim$(Split(Source, "input_range")(1)))
LowerRange = Fields(1)
UpperRange = Fields(2)
End If

Note that the "extra" quote marks in the Source assignment statement are
required in a String constant assignment (used here for example purposes) in
order to keep the quote marks in the String where you showed them in your
posting. They would not be required if the Source string came in via a
TextBox or was read in from a file.

Rick