A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Excel » Worksheet Functions
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Finding the Location within an Array



 
 
Thread Tools Display Modes
  #1  
Old December 5th, 2007, 11:45 AM posted to microsoft.public.excel.worksheet.functions
Sh0t2bts
external usenet poster
 
Posts: 9
Default Finding the Location within an Array

Hi All,

Thanks to this Group I was provided a snipit of code to find out is a
value is within my Array:-
If Not IsError(Application.Match(some_value, myArray,0)) Then
Do somthing
else
Do somthing else
End If

This works Great Thank You.

Is it possible to find out the location of the value without having to
loop through each entry?

Many Thanks

Mark
  #2  
Old December 5th, 2007, 12:57 PM posted to microsoft.public.excel.worksheet.functions
Dave Peterson
external usenet poster
 
Posts: 19,791
Default Finding the Location within an Array

dim res as variant
res = application.match(some_value, myArray,0)
if iserror(res) then
'no match
else
msgbox res
end if



Sh0t2bts wrote:

Hi All,

Thanks to this Group I was provided a snipit of code to find out is a
value is within my Array:-
If Not IsError(Application.Match(some_value, myArray,0)) Then
Do somthing
else
Do somthing else
End If

This works Great Thank You.

Is it possible to find out the location of the value without having to
loop through each entry?

Many Thanks

Mark


--

Dave Peterson
  #3  
Old December 5th, 2007, 01:06 PM posted to microsoft.public.excel.worksheet.functions
Ron Rosenfeld
external usenet poster
 
Posts: 3,719
Default Finding the Location within an Array

On Wed, 5 Dec 2007 03:45:33 -0800 (PST), Sh0t2bts wrote:

Hi All,

Thanks to this Group I was provided a snipit of code to find out is a
value is within my Array:-
If Not IsError(Application.Match(some_value, myArray,0)) Then
Do somthing
else
Do somthing else
End If

This works Great Thank You.

Is it possible to find out the location of the value without having to
loop through each entry?

Many Thanks

Mark


You merely return the result of your Match function.

=====================================
Option Explicit
Sub foo()
Dim bar As Variant
Dim foobar
bar = Array(1, 5, 10, 15, 20)

foobar = 10

Debug.Print "Value foobar", foobar, "Location " & _
Application.WorksheetFunction.Match(foobar, bar, 0)

End Sub
==============================

Note that MATCH will return a 1's based count. So if your array is 0 based,
then the "location" within the array will be offset by 1.
--ron
 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


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


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.