View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
[email protected] meh2030@gmail.com is offline
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