Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default string matching

Dear all:
I have newbie question on how to match strings with excel macro.
What I want is to match a word against different elements of an array
and do something with it if matches.

Lets say I have an array like the one below and now I want to output
(msgbox) every time the string "apple" appears. How would I do this?
In perl it would be trivial with something like =~ /.apple./

----------------------------------------------
Dim arr() As Variant
ReDim arr(3)
arr(1) = "apple tree"
arr(2) = "I like apples"
arr(3) = "pears are good for you"

For x = 1 To 3
If arr(x) = "*apple*" Then
msgbox arr(x)
end if

Next
----------------------------------------------


Unfortunenly the code above does not work. Any help?

thanks. alex

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default string matching

Hi Alex,

Try something like:

'=============
Public Sub Tester()
Dim x As Long
Dim arr() As Variant
ReDim arr(3)
arr(1) = "apple tree"
arr(2) = "I like apples"
arr(3) = "pears are good for you"

For x = 1 To 3
If InStr(1, arr(x), "apple", vbTextCompare) Then
MsgBox arr(x)
End If

Next x
End Sub
'<<=============


---
Regards,
Norman

"Alex Lee" wrote in message
oups.com...
Dear all:
I have newbie question on how to match strings with excel macro.
What I want is to match a word against different elements of an array
and do something with it if matches.

Lets say I have an array like the one below and now I want to output
(msgbox) every time the string "apple" appears. How would I do this?
In perl it would be trivial with something like =~ /.apple./

----------------------------------------------
Dim arr() As Variant
ReDim arr(3)
arr(1) = "apple tree"
arr(2) = "I like apples"
arr(3) = "pears are good for you"

For x = 1 To 3
If arr(x) = "*apple*" Then
msgbox arr(x)
end if

Next
----------------------------------------------


Unfortunenly the code above does not work. Any help?

thanks. alex



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default string matching

Hi Alex,

I should, however, have added that, unless you have
declared Option Base as 1 at the head of your module,
your instruction:

ReDim arr(3)


is equivalent to:

ReDim arr(0 to 3)



---
Regards,
Norman


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,202
Default string matching

I have newbie question on how to match strings with excel macro.
What I want is to match a word against different elements of an array
and do something with it if matches.

Lets say I have an array like the one below and now I want to output
(msgbox) every time the string "apple" appears. How would I do this?
In perl it would be trivial with something like =~ /.apple./

----------------------------------------------
Dim arr() As Variant
ReDim arr(3)
arr(1) = "apple tree"
arr(2) = "I like apples"
arr(3) = "pears are good for you"

For x = 1 To 3
If arr(x) = "*apple*" Then
msgbox arr(x)
end if

Next
----------------------------------------------


You could use the InStr method Norman posted, or one that looks almost like
a regular expression...

For x = 1 To 3
If arr(x) Like "*[aA]pple*" Then
MsgBox arr(x)
End If
Next x

where the [aA] allows "apple" to start with either a lower or upper case
"A".

Another possibility is to use the Filter function to create an array of just
elements from the original array that contain the search word (apple, in
your case) and also note the alternate way of assigning values to your
array...

Public Sub Tester()
Dim X As Long
Dim Arr() As String
Dim SubArr
Arr = Split("apple tree|I like apples|pears are good for you", "|")
SubArr = Filter(Arr, "apple", , vbTextCompare)
For X = 0 To UBound(SubArr)
MsgBox Arr(X)
Next X
End Sub


Rick

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,202
Default string matching

Typed too fast... see inline correction.

Another possibility is to use the Filter function to create an array of
just elements from the original array that contain the search word (apple,
in your case) and also note the alternate way of assigning values to your
array...

Public Sub Tester()
Dim X As Long
Dim Arr() As String
Dim SubArr


The above should be... Dim SubArr() As String

Arr = Split("apple tree|I like apples|pears are good for you", "|")
SubArr = Filter(Arr, "apple", , vbTextCompare)
For X = 0 To UBound(SubArr)
MsgBox Arr(X)
Next X
End Sub


Rick



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default string matching

Hi Norman:
that totally worked! thanks. I've searched google for an hr but could
not find a solution.

alex

On Jun 5, 1:08 pm, "Norman Jones"
wrote:
Hi Alex,

Try something like:

'=============
Public Sub Tester()
Dim x As Long
Dim arr() As Variant
ReDim arr(3)
arr(1) = "apple tree"
arr(2) = "I like apples"
arr(3) = "pears are good for you"

For x = 1 To 3
If InStr(1, arr(x), "apple", vbTextCompare) Then
MsgBox arr(x)
End If

Next x
End Sub
'<<=============

---
Regards,
Norman

"Alex Lee" wrote in message

oups.com...



Dear all:
I have newbie question on how to match strings with excel macro.
What I want is to match a word against different elements of an array
and do something with it if matches.


Lets say I have an array like the one below and now I want to output
(msgbox) every time the string "apple" appears. How would I do this?
In perl it would be trivial with something like =~ /.apple./


----------------------------------------------
Dim arr() As Variant
ReDim arr(3)
arr(1) = "apple tree"
arr(2) = "I like apples"
arr(3) = "pears are good for you"


For x = 1 To 3
If arr(x) = "*apple*" Then
msgbox arr(x)
end if


Next
----------------------------------------------


Unfortunenly the code above does not work. Any help?


thanks. alex- Hide quoted text -


- Show quoted text -



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default string matching

Rick,
thanks for your help. You are right about this method being more
regular expression like. Actually I prefer this method because of the
similiartiy to regex.

alex

On Jun 5, 1:30 pm, "Rick Rothstein \(MVP - VB\)"
wrote:
I have newbie question on how to match strings with excel macro.
What I want is to match a word against different elements of an array
and do something with it if matches.


Lets say I have an array like the one below and now I want to output
(msgbox) every time the string "apple" appears. How would I do this?
In perl it would be trivial with something like =~ /.apple./


----------------------------------------------
Dim arr() As Variant
ReDim arr(3)
arr(1) = "apple tree"
arr(2) = "I like apples"
arr(3) = "pears are good for you"


For x = 1 To 3
If arr(x) = "*apple*" Then
msgbox arr(x)
end if


Next
----------------------------------------------


You could use the InStr method Norman posted, or one that looks almost like
a regular expression...

For x = 1 To 3
If arr(x) Like "*[aA]pple*" Then
MsgBox arr(x)
End If
Next x

where the [aA] allows "apple" to start with either a lower or upper case
"A".

Another possibility is to use the Filter function to create an array of just
elements from the original array that contain the search word (apple, in
your case) and also note the alternate way of assigning values to your
array...

Public Sub Tester()
Dim X As Long
Dim Arr() As String
Dim SubArr
Arr = Split("apple tree|I like apples|pears are good for you", "|")
SubArr = Filter(Arr, "apple", , vbTextCompare)
For X = 0 To UBound(SubArr)
MsgBox Arr(X)
Next X
End Sub

Rick- Hide quoted text -

- Show quoted text -



  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,202
Default string matching

thanks for your help. You are right about this method being more
regular expression like. Actually I prefer this method because of the
similiartiy to regex.


Just so you know, though, the InStr test is "faster" than the Like operator
comparison; but you wouldn't see the difference unless you were running a
huge loop. You might want to look again at the Filter function as that is
remarkably flexible in finding sub-strings within String arrays.

Rick

  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,202
Default string matching

You might want to look again at the Filter function as that is remarkably
flexible in finding sub-strings within String arrays.


And the reason why is that you don't have to examine each individual array
element in a loop looking for matching substrings... true, Filter must do
that behind the scenes, but it is so much quicker in doing so than a VBA
loop can ever be.

Rick

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
index and matching with string search! via135 Excel Worksheet Functions 4 April 16th 08 07:03 PM
string matching adimar Excel Worksheet Functions 2 April 14th 08 10:05 PM
Building Sum by Matching String guruk Excel Discussion (Misc queries) 1 July 10th 06 12:55 PM
regex string matching Jamie Martin[_2_] Excel Programming 3 September 26th 03 09:28 PM
Wildcard pattern matching and replace in string Janelle Excel Programming 0 August 7th 03 10:43 PM


All times are GMT +1. The time now is 06:08 PM.

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"