View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Mike H Mike H is offline
external usenet poster
 
Posts: 11,501
Default Find cells that contain text

Hi,

Two text strings

AI_AIMS_
AI_AIMS_WWS

The INSTR function your using with OR will 'never' find the second string
(AI_AIMS_WWS) because the first one (AI_AIMS_) will already have evaluated as
TRUE and INSTR stops evaluating as soon as TRUE condition is encountered.

If 'Many' variable begin A1_MMS_ then perhaps you can ignore looking for
this bit and instead just look for the last 3 characters.

--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.


"AMH" wrote:

Thanks Mike tried that but still same result, I was not using string it was
just an example, here is the real code I have tried using :

If InStr(1, Cells(bb, 65), opasheetcode1, vbTextCompare) 0 Or InStr(1,
Cells(bb, 65), opasheetcode2, vbTextCompare) 0 Then

With opasheetcode1 or opasheetcode2 being two different strings of text I am
looking for, the 1st string contains AI_AIMS which it recognises, however I
also want to identify AI_AIMS_WWS (which is one of many variables to end of
string AI_AIMS), which it does not recognise thus I dont get any data back
for the 2nd string


"Mike H" wrote:

Hi,

Try this

If InStr(1, Cells(bb, 1), mystring, vbTextCompare) 0 Then

Note this will find a string with a longer string and isn't case sensitive.
Also you were using a variable called 'string' and this is a reserved word so
you can't do that
--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.


"AMH" wrote:

Hi, as part of a loop I am trying to find cells that contain a string and
then copy some adjacent cells into another sheet. I can do the copy part no
problem but I am struggling with finding the string, I am using the InStr
command but not sure if I ma making this to complicated, this is code I am
struggling with :

If InStr(string, Cells(bb, 65)) = 1 Then
With string being the string I am searching for and bb being the row number
and part of the loop

The formula will recognise a complete string, but not part of string i.e.

String = abcd_
Cell contains abcd_ match and I can copy data
If cell contains abcd_efg no match and I want to take data from this row

I hope this makes sense and someone can help me

Thanks