+ Reply to Thread
Results 1 to 5 of 5

Problem using .Index in Array

Hybrid View

  1. #1
    Registered User
    Join Date
    02-09-2011
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2003
    Posts
    2

    Problem using .Index in Array

    I am having a strange issue when using .index with an array in vba.

    I am using ado to obtain a recordset from a closed workbook. This is a function that I have defined that returns an array to the main sub. The array is then transposed into another array to restructure into the row, col format. Everything works fine up until i try to utilize the .Index function.

    When i try to use the Application.Index function, it never returns a value from the transposed array.

    What I have done to work around this is to copy the transposed array into a worksheet, then copy the range values into yet another array. When doing this, I am able to fully utilize the .Index function.

    So the index function will NOT work unless I copy the data to a worksheet, then back into an array.

    Does this sound right?

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Problem using .Index in Array

    Hello thekeel,

    Welcome to the Forum!

    In my experience, it would be better to use the WorksheetFunction.Index method in VBA. The Application.Function calls are not supported by VBA. You can invoke them but the object model only supports the WorksheetFunction method. Try using this and let me know the results.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Problem using .Index in Array

    As far as I know application.index and worksheetfunction.index are the same (like cells(1,1), range("A1") and [A1] are the same).

    Sub tst()
      sn = Range("A1:F10")
      MsgBox Application.Index(sn, 3, 4)
      MsgBox WorksheetFunction.Index(sn, 3, 4)
    End Sub



  4. #4
    Registered User
    Join Date
    02-09-2011
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Problem using .Index in Array

    Quote Originally Posted by Leith Ross View Post
    Hello thekeel,

    Welcome to the Forum!

    In my experience, it would be better to use the WorksheetFunction.Index method in VBA. The Application.Function calls are not supported by VBA. You can invoke them but the object model only supports the WorksheetFunction method. Try using this and let me know the results.
    Thanks for your help.

    I have tried using WorksheetFunction with the same results. Its really odd, when I load the array from a RecordSet, neither .Index or .Match will work.

    However, if I load the array from a worksheet, both functions work fine.

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Problem using .Index in Array

    Hello thekeel,

    I am not an SQL expert but I would think that an SQL array would not be any different from a VBA array. I have used the WorksheetFunction.Index call many times on arrays in VBA with no problem.

    It could be failing because the array you are returning from the record set is a 1-D array. Arrays returned from worksheets are always 2-D.

+ 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