Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default InStr function question


I am using Excel 2002

I have this problem with Instr. Imagine a cell that contains the
following text

11,22,33

If I use the following command, instr(1,activecell.Value,"2",0)

The function returns a 4.

But, what if I wanted to function to look for exactly a 2? Because 2 is
part of 22. The function found it.

Is there a better function that will help me find exactly what I am
looking for ?

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default InStr function question

sStr = "11,22,33"
if instr(1,sStr,",2,") or instr(1,sStr,"2,") = 1 or instr(1,sStr,",2") =
len(sStr)-1 then

--
Regards,
Tom Ogilvy


wrote in message
oups.com...

I am using Excel 2002

I have this problem with Instr. Imagine a cell that contains the
following text

11,22,33

If I use the following command, instr(1,activecell.Value,"2",0)

The function returns a 4.

But, what if I wanted to function to look for exactly a 2? Because 2 is
part of 22. The function found it.

Is there a better function that will help me find exactly what I am
looking for ?



  #3   Report Post  
Posted to microsoft.public.excel.programming
ben ben is offline
external usenet poster
 
Posts: 232
Default InStr function question

Tom,
Wouldn't that last instr function still cause it to find ",22" or
",234443" ?

--
When you lose your mind, you free your life.


"Tom Ogilvy" wrote:

sStr = "11,22,33"
if instr(1,sStr,",2,") or instr(1,sStr,"2,") = 1 or instr(1,sStr,",2") =
len(sStr)-1 then

--
Regards,
Tom Ogilvy


wrote in message
oups.com...

I am using Excel 2002

I have this problem with Instr. Imagine a cell that contains the
following text

11,22,33

If I use the following command, instr(1,activecell.Value,"2",0)

The function returns a 4.

But, what if I wanted to function to look for exactly a 2? Because 2 is
part of 22. The function found it.

Is there a better function that will help me find exactly what I am
looking for ?




  #4   Report Post  
Posted to microsoft.public.excel.programming
ben ben is offline
external usenet poster
 
Posts: 232
Default InStr function question

nevermind, i see what you did. Thanks for learning us yokels Tom. =)
--
When you lose your mind, you free your life.


"ben" wrote:

Tom,
Wouldn't that last instr function still cause it to find ",22" or
",234443" ?

--
When you lose your mind, you free your life.


"Tom Ogilvy" wrote:

sStr = "11,22,33"
if instr(1,sStr,",2,") or instr(1,sStr,"2,") = 1 or instr(1,sStr,",2") =
len(sStr)-1 then

--
Regards,
Tom Ogilvy


wrote in message
oups.com...

I am using Excel 2002

I have this problem with Instr. Imagine a cell that contains the
following text

11,22,33

If I use the following command, instr(1,activecell.Value,"2",0)

The function returns a 4.

But, what if I wanted to function to look for exactly a 2? Because 2 is
part of 22. The function found it.

Is there a better function that will help me find exactly what I am
looking for ?




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default InStr function question

Simpler

sStr = "," & ActiveCell.Value & ","
if instr(1,sStr,",2,") then
msgbox "contains 2"
End if

--
Regards,
Tom Ogilvy


"Tom Ogilvy" wrote in message
...
sStr = "11,22,33"
if instr(1,sStr,",2,") or instr(1,sStr,"2,") = 1 or instr(1,sStr,",2") =
len(sStr)-1 then

--
Regards,
Tom Ogilvy


wrote in message
oups.com...

I am using Excel 2002

I have this problem with Instr. Imagine a cell that contains the
following text

11,22,33

If I use the following command, instr(1,activecell.Value,"2",0)

The function returns a 4.

But, what if I wanted to function to look for exactly a 2? Because 2 is
part of 22. The function found it.

Is there a better function that will help me find exactly what I am
looking for ?







  #6   Report Post  
Posted to microsoft.public.excel.programming
ben ben is offline
external usenet poster
 
Posts: 232
Default InStr function question

Unless somebody has a better way


(11,22,33) is active cell

i = instr(1,activecell,"2")
if mid(activecell,i+1,1) = "," then
'it is a two
else
it is not exactly two
end if



--
When you lose your mind, you free your life.


" wrote:


I am using Excel 2002

I have this problem with Instr. Imagine a cell that contains the
following text

11,22,33

If I use the following command, instr(1,activecell.Value,"2",0)

The function returns a 4.

But, what if I wanted to function to look for exactly a 2? Because 2 is
part of 22. The function found it.

Is there a better function that will help me find exactly what I am
looking for ?


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default InStr function question

That test would fail for

11,33,2

--
Regards,
Tom Ogilvy


"ben" (remove this if mailing direct) wrote in message
...
Unless somebody has a better way


(11,22,33) is active cell

i = instr(1,activecell,"2")
if mid(activecell,i+1,1) = "," then
'it is a two
else
it is not exactly two
end if



--
When you lose your mind, you free your life.


" wrote:


I am using Excel 2002

I have this problem with Instr. Imagine a cell that contains the
following text

11,22,33

If I use the following command, instr(1,activecell.Value,"2",0)

The function returns a 4.

But, what if I wanted to function to look for exactly a 2? Because 2 is
part of 22. The function found it.

Is there a better function that will help me find exactly what I am
looking for ?




  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,120
Default InStr function question

This seems to work

Function Singleton(val, test)
Dim ipos As Long
Dim iStart As Long
Dim fOK As Boolean

iStart = 1
Do
ipos = InStr(iStart, val, test, 0)
fOK = ipos < 0 And (ipos = Len(val) Or Mid(val, ipos + 1, 1) <
test)
iStart = ipos + 2
Loop Until fOK Or ipos = 0

If ipos = 0 Then
Singleton = False
Else
Singleton = ipos
End If
End Function

Sub testSingleton()
Debug.Print Singleton("1,2,3", "2")
Debug.Print Singleton("11,22,33", "2")
Debug.Print Singleton("11,2,33", "2")
End Sub


--
HTH

Bob Phillips

wrote in message
oups.com...

I am using Excel 2002

I have this problem with Instr. Imagine a cell that contains the
following text

11,22,33

If I use the following command, instr(1,activecell.Value,"2",0)

The function returns a 4.

But, what if I wanted to function to look for exactly a 2? Because 2 is
part of 22. The function found it.

Is there a better function that will help me find exactly what I am
looking for ?



  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 93
Default InStr function question

The function returns 4 because "2" is on 4th position in your text. It
was looking exactly for "2".

If you want ",2," to be found use instr(1 , activecell.value ,
",2," , 0)

  #10   Report Post  
Posted to microsoft.public.excel.programming
ben ben is offline
external usenet poster
 
Posts: 232
Default InStr function question

That won't find "2," or ",2"
--
When you lose your mind, you free your life.


" wrote:


I am using Excel 2002

I have this problem with Instr. Imagine a cell that contains the
following text

11,22,33

If I use the following command, instr(1,activecell.Value,"2",0)

The function returns a 4.

But, what if I wanted to function to look for exactly a 2? Because 2 is
part of 22. The function found it.

Is there a better function that will help me find exactly what I am
looking for ?




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
Use InStr function in formula? Lee Hunter Excel Worksheet Functions 8 May 8th 23 03:45 AM
Help with VBA InStr() function EagleOne Excel Discussion (Misc queries) 10 April 12th 07 02:47 PM
Where is the Instr() function in Excel 2003? chem21 Excel Discussion (Misc queries) 3 October 9th 06 03:49 PM
Instr function problem Ross Withey Excel Programming 9 November 14th 03 08:02 AM


All times are GMT +1. The time now is 06:29 AM.

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

About Us

"It's about Microsoft Excel"