+ Reply to Thread
Results 1 to 3 of 3

most efficient and quickest way to populate data from sheet to array

  1. #1
    Registered User
    Join Date
    05-15-2016
    Location
    Sydney
    MS-Off Ver
    excel 2013
    Posts
    21

    most efficient and quickest way to populate data from sheet to array

    Hi i was told the following is the most efficient way of getting data from a sheet into an array

    dim arr as variant
    arr = range("A1:J200").value

    Using the above code my macro retrieves data from 4 different sheets(approx 200 rows 10 columns each sheet!!!) and then puts them in 4 different variants. I then use these 4 variants , manipulate data and put all of them into one variant which i then transfer on to one sheet at once.

    i have also been told to avoid using variants as they are generally slow?! is that right?

    What are some of the other ways i can achieve the above?

    I was thinking of using for loops but i find them extremely slow. each worksheet cell to array is slow...

    Please advise best approach based on your experiences.. Thanks in advance.

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: most efficient and quickest way to populate data from sheet to array

    Quote Originally Posted by twozedz View Post
    Hi i was told the following is the most efficient way of getting data from a sheet into an array

    dim arr as variant
    arr = range("A1:J200").value

    Using the above code my macro retrieves data from 4 different sheets(approx 200 rows 10 columns each sheet!!!) and then puts them in 4 different variants. I then use these 4 variants , manipulate data and put all of them into one variant which i then transfer on to one sheet at once.
    I agree. This is probably the most efficient method without knowing how you "manipulate" the data.

    i have also been told to avoid using variants as they are generally slow?! is that right?
    No. Variants are not slow. Use them where appropriate as in this case.

    What are some of the other ways i can achieve the above?
    This s probably the best method without knowing further details.

    I was thinking of using for loops but i find them extremely slow. Each worksheet cell to array is slow...
    Loops themselves are not slow. What you do within the loop maybe slow. Looping through cells on a sheet is slow because each reference to a cell is expensive; not the loop itself. The idea is to minimize the read\writes to the sheet. Your code above only has 4 reads and 1 write. That's what makes it efficient.
    Last edited by AlphaFrog; 08-14-2016 at 10:21 AM.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    05-15-2016
    Location
    Sydney
    MS-Off Ver
    excel 2013
    Posts
    21

    Re: most efficient and quickest way to populate data from sheet to array

    Quote Originally Posted by AlphaFrog View Post
    I agree. This is probably the most efficient method without knowing how you "manipulate" the data.


    No. Variants are not slow. Use them where appropriate as in this case.


    This s probably the best method without knowing further details.


    Loops themselves are not slow. What you do within the loop maybe slow. Looping through cells on a sheet is slow because each reference to a cell is expensive; not the loop itself. The idea is to minimize the read\writes to the sheet. Your code above only has 4 reads and 1 write. That's what makes it efficient.
    Thank you. Your input appreciated!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 8
    Last Post: 02-09-2015, 09:45 PM
  2. [SOLVED] Is there a more efficient way of populating a VBA array
    By Philb1 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-27-2014, 06:40 PM
  3. Using array to make 'for' loop more efficient
    By rynofrowan in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-31-2014, 05:05 AM
  4. Replies: 1
    Last Post: 09-16-2014, 10:02 AM
  5. More efficient way to find max value for an array of values
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-02-2013, 11:38 AM
  6. Quickest way to get code onto every sheet?
    By kirsty in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-17-2010, 08:50 PM
  7. [SOLVED] Quickest Way of making an Array
    By WhytheQ in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-20-2006, 02:15 PM

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