+ Reply to Thread
Results 1 to 3 of 3

How can one populate a long array?

  1. #1
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    How can one populate a long array?

    I'm trying to figure out how to populate a long array.
    The lines commented out did not work.
    I'm able to populate a long array one element at a time, but I can't figure out how to populate the whole array at once.
    I've tried Split & Array, and neither seemed to work.

    I also would like to refer to the index by hex numbers, that seems to be working.

    Please Login or Register  to view this content.

  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: How can one populate a long array?

    Hello SteveM,

    VBA handles arrays a little oddly. Generally, you need to create a Variant Array. Here is an example.
    Please Login or Register  to view this content.
    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
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: How can one populate a long array?

    ' Load nArray with a variant Array of Long values.
    nArray = Array(9&, 8&, 7&, 6&, 5&, 4&, 3&, 2&, 1&, 0&)
    Except it is not an array of longs, but an array of Variants with long values. If one attempts to pass one element of the array to a function which needs a long, one has to convert the variant to a long, such as: CLng(nArray(i)), otherwise the compiler produces an error because of a type mismatch.

    So since it is impossible to populate a long array at one go (as opposed to adding a value to each element one at a time), which would be better:

    Please Login or Register  to view this content.
    Would it be better (as in faster) to use a Variant array with Array, or a String array with Split? Or does it make any difference?

    In the project I'm working on, I need two arrays. One has around 256 elements, and the other has about half that.

    I originally wrote the function as one large Select Case statement with somewhere around 350-400 case statements. But I figured (whether I'm right or wrong, I don't know) that such a large Select Case statement would be much slower than using two arrays. With an array, I can calculate which element I need and then just read that element. The elements in these arrays will have values from 1 to 50 (hence my desire to populate a long array).

+ 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