Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 60
Default exact match in a string

i1 = " week1, week2, week5, week16, week18, week24 "

i2 = " week16 "

If InStr( i1, i2 ) 0 Then

When I put above code in a loop to check all rows in my sheet not only
" week16 " responds but also " week1 " comes in.

How can I avoid "week1" ?
Or " week2 " while looping for " week24" ?

Any idea ? Thank you very much.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 60
Default exact match in a string

On May 13, 2:27*pm, " wrote:
i1 = *" week1, week2, week5, week16, week18, week24 "

i2 = *" week16 "

If InStr( i1, i2 ) 0 Then

When I put above code in a loop to check all rows in my sheet not only
" week16 " responds but also " week1 " comes in.

How can I avoid "week1" ?
Or " week2 " while looping for " week24" ?

Any idea ? Thank you very much.


Oppps !

i1 = " week5, week16, week18, week24 "

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default exact match in a string

Hi,

With the spaces removed this nor correctly returns 19 for me

i1 = "week1,week2,week5,week16,week18,week24"
i2 = "week16"
If InStr(i1, i2) Then
x = InStr(i1, i2)
MsgBox x
End If

Mike

" wrote:

i1 = " week1, week2, week5, week16, week18, week24 "

i2 = " week16 "

If InStr( i1, i2 ) 0 Then

When I put above code in a loop to check all rows in my sheet not only
" week16 " responds but also " week1 " comes in.

How can I avoid "week1" ?
Or " week2 " while looping for " week24" ?

Any idea ? Thank you very much.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 60
Default exact match in a string

Thank you very much Mike.
What i meant was:

week 16, week 26, week 30 is the string of a multiple selection.
So my purpose is to sum the sales of weeks 16,26 and 30.
I use this "..If InStr( i1, i2 ) 0 Then.." and week 1 joins the
club.

Thank you.



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 135
Default exact match in a string

On May 13, 9:24*am, " wrote:
Thank you very much Mike.
What i meant was:

week 16, week 26, week 30 is the string of a multiple selection.
So my purpose is to sum the sales of weeks 16,26 and 30.
I use this *"..If InStr( i1, i2 ) 0 Then.." and week 1 joins the
club.

Thank you.


Rumkus,

You could either add another integer digit to your "week" name for
single digits, e.g. week05 instead of week5, and use StrComp (see
"TestStrComp" below), or you could parse off the number and do a
number comparison (see "TestParse" below and read the commented
assumption).

Best,

Matthew Herbert

Sub TestStrComp()
Dim strOne As String
Dim strTwo As String
Dim varSplit As Variant
Dim intI As Integer

strOne = " week05, week16, week18, week24 "
strTwo = " week16 "

varSplit = Split(strOne, ",")

For intI = LBound(varSplit) To UBound(varSplit)
If StrComp(Trim(strTwo), Trim(varSplit(intI)), vbTextCompare) = -1
Then
Debug.Print "strTwo:"; strTwo; " | strOne:"; varSplit(intI)
End If
Next
End Sub

Sub TestParse()
Dim strOne As String
Dim strTwo As String
Dim intOne As Integer
Dim intTwo As Integer
Dim strCnst As String
Dim varSplit As Variant
Dim intI As Integer

'Assumes ALL strings start with the "week" text,
' and contain "week" text
strCnst = "week"
strOne = " week5, week16, week18, week24 "
strTwo = " week16 "

strTwo = Trim(strTwo)
intTwo = CInt(Replace(strTwo, strCnst, ""))
varSplit = Split(strOne, ",")

For intI = LBound(varSplit) To UBound(varSplit)
strOne = Trim(varSplit(intI))
intOne = CInt(Replace(strOne, strCnst, ""))

If intOne intTwo Then
Debug.Print "strTwo:"; strTwo; " | strOne:"; varSplit(intI)
End If
Next

End Sub


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 60
Default exact match in a string

Sub TestStrComp() does it all !!

Thank you very much Matt for your time.
I really appreciated.

Kindest regards
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
Find an exact match and go to that match Heath Excel Discussion (Misc queries) 0 February 12th 09 02:28 AM
Find Exact Match using INDEX, MATCH DoubleUU Excel Worksheet Functions 3 August 15th 08 02:42 PM
vlookup and finding text string that's not an exact match my Excel Discussion (Misc queries) 4 July 31st 07 05:04 PM
Getting an exact match Roland Excel Worksheet Functions 2 January 25th 05 05:00 AM
Test for exact match in string Jeremy Gollehon[_2_] Excel Programming 3 August 24th 04 12:58 AM


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