+ Reply to Thread
Results 1 to 9 of 9

find the index number for an array element

Hybrid View

Guest find the index number for an... 01-23-2006, 11:25 AM
Guest Re: find the index number for... 01-23-2006, 11:35 AM
Guest Re: find the index number for... 01-23-2006, 11:45 AM
Peter Bernadyne You know, it's funny, I would... 03-10-2006, 12:48 PM
Guest Re: find the index number for... 03-10-2006, 04:00 PM
  1. #1
    Dick Minter
    Guest

    find the index number for an array element

    I want to lookup/find a specific value in an array and return that values
    index number. In this case, the lookup list is the first column of 3 column
    array. Suggestions?

    DM

  2. #2
    Tom Ogilvy
    Guest

    Re: find the index number for an array element

    Do you mean a VBA array or a range of cells on a worksheet

    for a VBA array:

    Dim v as Variant, i as Long, idx as Long
    v = Range("A1:C200")
    for i = 1 to 200
    if v(i,1) = "dog" then
    idx = i
    exit for
    end if
    Next
    if idx <> 0 then
    msgbox "found at index " & idx
    else
    msgbox "Not found"
    end if

    for a worksheet, use the match worksheet function.

    --
    Regards,
    Tom Ogilvy



    "**** Minter" <DickMinter@discussions.microsoft.com> wrote in message
    news:11A83F44-1887-4AED-963B-E4457301742A@microsoft.com...
    > I want to lookup/find a specific value in an array and return that values
    > index number. In this case, the lookup list is the first column of 3

    column
    > array. Suggestions?
    >
    > DM




  3. #3
    Dick Minter
    Guest

    Re: find the index number for an array element

    I meant a VBA array. Thanks, Tom.

    "Tom Ogilvy" wrote:

    > Do you mean a VBA array or a range of cells on a worksheet
    >
    > for a VBA array:
    >
    > Dim v as Variant, i as Long, idx as Long
    > v = Range("A1:C200")
    > for i = 1 to 200
    > if v(i,1) = "dog" then
    > idx = i
    > exit for
    > end if
    > Next
    > if idx <> 0 then
    > msgbox "found at index " & idx
    > else
    > msgbox "Not found"
    > end if
    >
    > for a worksheet, use the match worksheet function.
    >
    > --
    > Regards,
    > Tom Ogilvy
    >
    >
    >
    > "**** Minter" <DickMinter@discussions.microsoft.com> wrote in message
    > news:11A83F44-1887-4AED-963B-E4457301742A@microsoft.com...
    > > I want to lookup/find a specific value in an array and return that values
    > > index number. In this case, the lookup list is the first column of 3

    > column
    > > array. Suggestions?
    > >
    > > DM

    >
    >
    >


  4. #4
    Registered User
    Join Date
    03-10-2004
    Posts
    55
    You know, it's funny, I would have thought that there would be a more efficient way of identifying an array element instead of having to resort to a loop, but I suppose there isn't.

    That said, is there anyway of referring to just 1 column of a multiple column array such as the one described? By this, I mean the entire range of values in that column, of course.

    So, if I had an array of (1 to 50, 1 to 3) for instance, is there a way of referencing all 50 elements in column 1, by any chance? I'm trying to get something like myarray(:,1) to indicate the first column (as in Matlab, per any Matlab users out there).

    Thanks,

    -Pete

  5. #5
    unavailable
    Guest

    Re: find the index number for an array element

    Watch for word wrap
    Sub test1()
    Dim arr
    arr = Range("A1:C50")
    arr1 = Application.Index(arr, 0, 1) '1st column
    arr2 = Application.Index(arr, 0, 2) '2nd column
    arr3 = Application.Index(arr, 0, 3) '3rd column
    Debug.Print Application.Index(Application.Index(arr, 0, 2), 2, 1)
    '2nd row 2nd column
    Debug.Print Application.Index(Application.Index(arr, 0, 1), 3, 1)
    '3rd row 1st column
    End Sub

    Alan Beban

    Peter Bernadyne wrote:
    > You know, it's funny, I would have thought that there would be a more
    > efficient way of identifying an array element instead of having to
    > resort to a loop, but I suppose there isn't.
    >
    > That said, is there anyway of referring to just 1 column of a multiple
    > column array such as the one described? By this, I mean the entire
    > range of values in that column, of course.
    >
    > So, if I had an array of (1 to 50, 1 to 3) for instance, is there a way
    > of referencing all 50 elements in column 1, by any chance? I'm trying
    > to get something like myarray(:,1) to indicate the first column (as in
    > Matlab, per any Matlab users out there).
    >
    > Thanks,
    >
    > -Pete
    >
    >


  6. #6
    Registered User
    Join Date
    03-10-2004
    Posts
    55
    That was very helpful, thank you very much.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1