ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Check if a String is inside an Array (https://www.excelbanter.com/excel-programming/275986-re-check-if-string-inside-array.html)

Dave Peterson[_3_]

Check if a String is inside an Array
 
You can use the worksheet function Index:

Option Explicit
Sub testme()

Dim res As Variant
Dim MyArray As Variant

MyArray = Array("Test", "This")

res = Application.Match("this", MyArray, 0)

If IsError(res) Then
MsgBox "Not in there"
Else
MsgBox "It's there and at position: " & res
End If

End Sub


Luis Carrion wrote:

Hi,
If I were to check if a variable: A="Test" is inside a list of
possible strings,Iīll do this:

Dim A as string
Dim B as variant
Dim MyArray As variant

MyArray=Array("Test", "This")
For Each B in MyArray
If A=B then Msgbox "Itīs inside the group
Next A

Is it possible to test this in other way; something like this
If A is in/like MyArray then....... ---without looping inside the
array

Thanks.
LC


--

Dave Peterson


Tom Ogilvy

Check if a String is inside an Array
 
You can use the worksheet function Index:

Did you mean the worksheet function Match?



--
Regards,
Tom Ogilvy

"Dave Peterson" wrote in message
...
You can use the worksheet function Index:

Option Explicit
Sub testme()

Dim res As Variant
Dim MyArray As Variant

MyArray = Array("Test", "This")

res = Application.Match("this", MyArray, 0)

If IsError(res) Then
MsgBox "Not in there"
Else
MsgBox "It's there and at position: " & res
End If

End Sub


Luis Carrion wrote:

Hi,
If I were to check if a variable: A="Test" is inside a list of
possible strings,Iīll do this:

Dim A as string
Dim B as variant
Dim MyArray As variant

MyArray=Array("Test", "This")
For Each B in MyArray
If A=B then Msgbox "Itīs inside the group
Next A

Is it possible to test this in other way; something like this
If A is in/like MyArray then....... ---without looping inside the
array

Thanks.
LC


--

Dave Peterson




Alan Beban[_3_]

Check if a String is inside an Array
 
As Tom Ogilvy has pointed out, you meant to refer to the worksheet
function Match.

In xl2000 SR-1 and previous, the code below will work on 1-dimensional
arrays of 5461 or fewer elements.

Alan Beban

Dave Peterson wrote:
You can use the worksheet function Index:

Option Explicit
Sub testme()

Dim res As Variant
Dim MyArray As Variant

MyArray = Array("Test", "This")

res = Application.Match("this", MyArray, 0)

If IsError(res) Then
MsgBox "Not in there"
Else
MsgBox "It's there and at position: " & res
End If

End Sub


Luis Carrion wrote:

Hi,
If I were to check if a variable: A="Test" is inside a list of
possible strings,Iīll do this:

Dim A as string
Dim B as variant
Dim MyArray As variant

MyArray=Array("Test", "This")
For Each B in MyArray
If A=B then Msgbox "Itīs inside the group
Next A

Is it possible to test this in other way; something like this
If A is in/like MyArray then....... ---without looping inside the
array

Thanks.
LC





Dave Peterson[_3_]

Check if a String is inside an Array
 
Yes, I did. Darn fingers.



Tom Ogilvy wrote:

You can use the worksheet function Index:


Did you mean the worksheet function Match?

--
Regards,
Tom Ogilvy

"Dave Peterson" wrote in message
...
You can use the worksheet function Index:

Option Explicit
Sub testme()

Dim res As Variant
Dim MyArray As Variant

MyArray = Array("Test", "This")

res = Application.Match("this", MyArray, 0)

If IsError(res) Then
MsgBox "Not in there"
Else
MsgBox "It's there and at position: " & res
End If

End Sub


Luis Carrion wrote:

Hi,
If I were to check if a variable: A="Test" is inside a list of
possible strings,Iīll do this:

Dim A as string
Dim B as variant
Dim MyArray As variant

MyArray=Array("Test", "This")
For Each B in MyArray
If A=B then Msgbox "Itīs inside the group
Next A

Is it possible to test this in other way; something like this
If A is in/like MyArray then....... ---without looping inside the
array

Thanks.
LC


--

Dave Peterson


--

Dave Peterson



All times are GMT +1. The time now is 09:43 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com