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

I Have an Array with 25 strings stored. I want to search this array for a
certain string and get the placement in the array where this string is
located. if MYArray() = ("Example", "Trial", "work".... Et Cetera) and i
want to find where "Trial" is in the Array, it would give me the integer of 2

Thanks in advance

Ironhydroxide
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default Search an Array

you can try something like this:

For i = LBound(arr) To UBound(arr)
If arr(i) = "YourText" Then
'If UCase(arr(i)) = "YOURTEXT" Then 'use this if you don't want case
sensitive
Debug.Print i +1
Exit For
End If
Next
End Sub

--

Gary Keramidas
Excel 2003


"ironhydroxide" wrote in message
...
I Have an Array with 25 strings stored. I want to search this array for a
certain string and get the placement in the array where this string is
located. if MYArray() = ("Example", "Trial", "work".... Et Cetera) and i
want to find where "Trial" is in the Array, it would give me the integer
of 2

Thanks in advance

Ironhydroxide


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 915
Default Search an Array

ironhydroxide wrote:
I Have an Array with 25 strings stored. I want to search this array for a
certain string and get the placement in the array where this string is
located. if MYArray() = ("Example", "Trial", "work".... Et Cetera) and i
want to find where "Trial" is in the Array, it would give me the integer of 2

Thanks in advance

Ironhydroxide


You have to loop through the array until you find what you are looking
for. This will work for 1-based arrays:

Function WhereIsIt(SomeArray As Variant, _
ThingToFind As String) As Long
Dim i As Long
i = 1
Do While i < UBound(SomeArray) And _
SomeArray(i) < ThingToFind
i = i + 1
Loop
If SomeArray(i) = ThingToFind Then WhereIsIt = i
End Function
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Search an Array

Dim myArr as variant
dim res as variant
dim myStr as string

myArr = array("example","trial", "work")

mystr = "trial"

res = application.match(mystr, myarr,0)
if isnumeric(res) then
msgbox res
else
msgbox "no match"
end if

ps. Application.match is not case sensitive. Trial, TRIAL, TrIaL, ... will all
be treated the same.

ironhydroxide wrote:

I Have an Array with 25 strings stored. I want to search this array for a
certain string and get the placement in the array where this string is
located. if MYArray() = ("Example", "Trial", "work".... Et Cetera) and i
want to find where "Trial" is in the Array, it would give me the integer of 2

Thanks in advance

Ironhydroxide


--

Dave Peterson
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Search an Array

Is your array a one-dimensional Sting array? If so, here is another method
to consider...

Function ArrayIndex(Word As String, Arr() As String) As Long
Dim Temp As String
Temp = Chr(1) & LCase(Join(Arr, Chr(1))) & Chr(1)
If InStr(Temp, Chr(1) & LCase(Word) & Chr(1)) = 0 Then Exit Function
ArrayIndex = UBound(Split(Split(Temp, LCase(Word))(0), Chr(1)))
End Function

Just pass the word you want to find and the array into the function and it
will returns the index position of that word within the array (it returns 0
if the word is not located within the array). Here is a simple example
showing its usage...

Sub TestIndexFinder()
Dim MYArr() As String
MYArr = Split("Example,Trial,work,book,left,right", ",")
Debug.Print ArrayIndex("trial", MYArr)
End Sub

--
Rick (MVP - Excel)


"ironhydroxide" wrote in message
...
I Have an Array with 25 strings stored. I want to search this array for a
certain string and get the placement in the array where this string is
located. if MYArray() = ("Example", "Trial", "work".... Et Cetera) and i
want to find where "Trial" is in the Array, it would give me the integer
of 2

Thanks in advance

Ironhydroxide




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 51
Default Search an Array

All very good suggestions. thanks to all who replied.


"Rick Rothstein" wrote:

Is your array a one-dimensional Sting array? If so, here is another method
to consider...

Function ArrayIndex(Word As String, Arr() As String) As Long
Dim Temp As String
Temp = Chr(1) & LCase(Join(Arr, Chr(1))) & Chr(1)
If InStr(Temp, Chr(1) & LCase(Word) & Chr(1)) = 0 Then Exit Function
ArrayIndex = UBound(Split(Split(Temp, LCase(Word))(0), Chr(1)))
End Function

Just pass the word you want to find and the array into the function and it
will returns the index position of that word within the array (it returns 0
if the word is not located within the array). Here is a simple example
showing its usage...

Sub TestIndexFinder()
Dim MYArr() As String
MYArr = Split("Example,Trial,work,book,left,right", ",")
Debug.Print ArrayIndex("trial", MYArr)
End Sub

--
Rick (MVP - Excel)


"ironhydroxide" wrote in message
...
I Have an Array with 25 strings stored. I want to search this array for a
certain string and get the placement in the array where this string is
located. if MYArray() = ("Example", "Trial", "work".... Et Cetera) and i
want to find where "Trial" is in the Array, it would give me the integer
of 2

Thanks in advance

Ironhydroxide



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
search array jchick0909 Excel Worksheet Functions 3 October 5th 07 07:25 PM
search an array for values contained in another array Cheer-Phil-ly Excel Programming 0 April 12th 07 09:44 PM
Search An Array in VBA Dean Hinson[_3_] Excel Programming 7 March 18th 05 02:08 PM
SEARCH EXCEL ARRAY FOR NON-VOIDS AND ENTER INTO ANOTHER ARRAY, ROSE THE RED Excel Programming 1 December 31st 04 06:01 PM
Search an array [email protected] Excel Programming 3 October 21st 03 08:47 PM


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