Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 83
Default Check if variable contains a string

I'm trying to write VBA code that will look at a String variable and tell me
whether or not that String contains another String €” without erroring out.
Variable X contains this string: =SUM(AAAA,BBBB)
Variable Y contains this string: PPPP
I want something that works sort of like this:
If Variable X contains Variable Y, Then Variable W = 1
If Variable X does not contain Variable Y, Then Variable W = 2
In the example above, W will equal 2, since X does not contain Y
If Y is BBBB then W will equal 1, because X does contain Y
Many thanks.

  #3   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 5,934
Default Check if variable contains a string

If I understand your question correctly...

W = 2 + (InStr(VariableX, VariableY) 0)

The InStr function, as I used it above, is case sensitive. If you want a
case insensitive test...

W = 2 + (InStr(1, VariableX, VariableY, vbTextCompare) 0)

--
Rick (MVP - Excel)


"Ted M H" wrote in message
...
I'm trying to write VBA code that will look at a String variable and tell
me
whether or not that String contains another String €” without erroring out.
Variable X contains this string: =SUM(AAAA,BBBB)
Variable Y contains this string: PPPP
I want something that works sort of like this:
If Variable X contains Variable Y, Then Variable W = 1
If Variable X does not contain Variable Y, Then Variable W = 2
In the example above, W will equal 2, since X does not contain Y
If Y is BBBB then W will equal 1, because X does contain Y
Many thanks.


  #4   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 83
Default Check if variable contains a string

This works perfectly (I used the case sensitive test). I don't yet
understand why, but my immediate problem is solved.

I spent a little time with VBA help on the InStr function, and I think I
understand how it works. What puzzles me is the 2 +.... and the 0....
additions to the statement. You've already solved my problem, but if you're
inclined to help educate a struggling programming hacker further I'd sure be
interested in understanding why the solution works.

Many thanks.

"Rick Rothstein" wrote:

If I understand your question correctly...

W = 2 + (InStr(VariableX, VariableY) 0)

The InStr function, as I used it above, is case sensitive. If you want a
case insensitive test...

W = 2 + (InStr(1, VariableX, VariableY, vbTextCompare) 0)

--
Rick (MVP - Excel)


"Ted M H" wrote in message
...
I'm trying to write VBA code that will look at a String variable and tell
me
whether or not that String contains another String €” without erroring out.
Variable X contains this string: =SUM(AAAA,BBBB)
Variable Y contains this string: PPPP
I want something that works sort of like this:
If Variable X contains Variable Y, Then Variable W = 1
If Variable X does not contain Variable Y, Then Variable W = 2
In the example above, W will equal 2, since X does not contain Y
If Y is BBBB then W will equal 1, because X does contain Y
Many thanks.



  #5   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 5,934
Default Check if variable contains a string

W = 2 + (InStr(VariableX, VariableY) 0)

if you're inclined to help educate a struggling programming
hacker further I'd sure be interested in understanding why
the solution works.


I'd be more than happy to help you. First off, the InStr function looks at a
text string (your VariableY) to see if it is a substring of another text
string (your VariableX)... if it is, the InStr function returns the position
that VariableY is located at within VariableX (this will be a positive
number greater than 0); and if it isn't, the InStr function returns 0. Okay,
now let's look at the part of my statement in parentheses...

(InStr(VariableX, VariableY) 0)

This is known as a logical expression... one item (the result from the InStr
function) is being tested to see if it is greater than 0. If the result from
the InStr function is greater than 0, then the logical expression evaluates
to True, otherwise it evaluates to False. In other words, if VariableY is
contained as a substring within VariableX, the logical expression will
evaluate to True. This logical expression stuff should be familiar to you as
you see it all the time in If..Then statements; for example...

If InStr(VariableX, VariableY) 0 Then
W = 1
Else
W = 2
End If

By the way, this If..Then construction is the long way of doing what my
one-line statement does. Okay, back to the explanation. There are two
possible results from evaluating the logical expression... True or False...
in VB (note this will be different than when logical expressions are
evaluated on a worksheet), when used in a mathematical expression, True
becomes -1 (minus one) and False becomes 0 (zero). When these values are
added to 2, the results will be...

Logical Expression False... W = 2 + False = 2 + 0 = 2

Logical Expression True... W = 2 + True = 2 + (-1) = 1

I hope the above makes what I did clearer for you.

--
Rick (MVP - Excel)


"Ted M H" wrote in message
...
This works perfectly (I used the case sensitive test). I don't yet
understand why, but my immediate problem is solved.

I spent a little time with VBA help on the InStr function, and I think I
understand how it works. What puzzles me is the 2 +.... and the 0....
additions to the statement. You've already solved my problem, but if
you're
inclined to help educate a struggling programming hacker further I'd sure
be
interested in understanding why the solution works.

Many thanks.

"Rick Rothstein" wrote:

If I understand your question correctly...

W = 2 + (InStr(VariableX, VariableY) 0)

The InStr function, as I used it above, is case sensitive. If you want a
case insensitive test...

W = 2 + (InStr(1, VariableX, VariableY, vbTextCompare) 0)

--
Rick (MVP - Excel)


"Ted M H" wrote in message
...
I'm trying to write VBA code that will look at a String variable and
tell
me
whether or not that String contains another String €” without erroring
out.
Variable X contains this string: =SUM(AAAA,BBBB)
Variable Y contains this string: PPPP
I want something that works sort of like this:
If Variable X contains Variable Y, Then Variable W = 1
If Variable X does not contain Variable Y, Then Variable W = 2
In the example above, W will equal 2, since X does not contain Y
If Y is BBBB then W will equal 1, because X does contain Y
Many thanks.






  #6   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 83
Default Check if variable contains a string

Hi Rick,

Very clear explanation. Very, very helpful. Thanks again for helping me.

Ted


"Rick Rothstein" wrote:

W = 2 + (InStr(VariableX, VariableY) 0)


if you're inclined to help educate a struggling programming
hacker further I'd sure be interested in understanding why
the solution works.


I'd be more than happy to help you. First off, the InStr function looks at a
text string (your VariableY) to see if it is a substring of another text
string (your VariableX)... if it is, the InStr function returns the position
that VariableY is located at within VariableX (this will be a positive
number greater than 0); and if it isn't, the InStr function returns 0. Okay,
now let's look at the part of my statement in parentheses...

(InStr(VariableX, VariableY) 0)

This is known as a logical expression... one item (the result from the InStr
function) is being tested to see if it is greater than 0. If the result from
the InStr function is greater than 0, then the logical expression evaluates
to True, otherwise it evaluates to False. In other words, if VariableY is
contained as a substring within VariableX, the logical expression will
evaluate to True. This logical expression stuff should be familiar to you as
you see it all the time in If..Then statements; for example...

If InStr(VariableX, VariableY) 0 Then
W = 1
Else
W = 2
End If

By the way, this If..Then construction is the long way of doing what my
one-line statement does. Okay, back to the explanation. There are two
possible results from evaluating the logical expression... True or False...
in VB (note this will be different than when logical expressions are
evaluated on a worksheet), when used in a mathematical expression, True
becomes -1 (minus one) and False becomes 0 (zero). When these values are
added to 2, the results will be...

Logical Expression False... W = 2 + False = 2 + 0 = 2

Logical Expression True... W = 2 + True = 2 + (-1) = 1

I hope the above makes what I did clearer for you.

--
Rick (MVP - Excel)


"Ted M H" wrote in message
...
This works perfectly (I used the case sensitive test). I don't yet
understand why, but my immediate problem is solved.

I spent a little time with VBA help on the InStr function, and I think I
understand how it works. What puzzles me is the 2 +.... and the 0....
additions to the statement. You've already solved my problem, but if
you're
inclined to help educate a struggling programming hacker further I'd sure
be
interested in understanding why the solution works.

Many thanks.

"Rick Rothstein" wrote:

If I understand your question correctly...

W = 2 + (InStr(VariableX, VariableY) 0)

The InStr function, as I used it above, is case sensitive. If you want a
case insensitive test...

W = 2 + (InStr(1, VariableX, VariableY, vbTextCompare) 0)

--
Rick (MVP - Excel)


"Ted M H" wrote in message
...
I'm trying to write VBA code that will look at a String variable and
tell
me
whether or not that String contains another String €” without erroring
out.
Variable X contains this string: =SUM(AAAA,BBBB)
Variable Y contains this string: PPPP
I want something that works sort of like this:
If Variable X contains Variable Y, Then Variable W = 1
If Variable X does not contain Variable Y, Then Variable W = 2
In the example above, W will equal 2, since X does not contain Y
If Y is BBBB then W will equal 1, because X does contain Y
Many thanks.





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
Variable string$ Ferdy New Users to Excel 3 November 26th 08 08:45 PM
Use a string as a Variable Name Brandt Excel Worksheet Functions 4 November 28th 07 06:01 PM
find a variable text string [email protected] Excel Discussion (Misc queries) 2 January 30th 07 07:20 PM
calling variable within string [email protected] Excel Worksheet Functions 8 May 4th 06 03:07 PM
Variable in string DevinC Excel Discussion (Misc queries) 5 January 26th 06 08:59 PM


All times are GMT +1. The time now is 05:07 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"