+ Reply to Thread
Results 1 to 8 of 8

Is there a more efficient way of populating a VBA array

  1. #1
    Valued Forum Contributor
    Join Date
    08-22-2011
    Location
    Auckland
    MS-Off Ver
    Excel 2019
    Posts
    716

    Is there a more efficient way of populating a VBA array

    I've started learning how to use arrays. I've got 40 variables that have values as the result of a macro run & I want to populate an array with them in the routine, without writing to the worksheet & lifting the values. I'm thinking there's got to be a better way to do it than this

    MyArray(0)=varname1
    MyArray(1)=varname2
    etc

    Any ideas please
    Thanks
    Last edited by Philb1; 11-27-2014 at 06:41 PM.

  2. #2
    Forum Contributor
    Join Date
    07-29-2014
    Location
    Oz
    MS-Off Ver
    2010
    Posts
    142

    Re: Is there a more efficient way of populating a VBA array

    Something like:
    Please Login or Register  to view this content.
    Last edited by Danerida; 11-26-2014 at 11:11 PM.
    Don't forget to ☆ me if I helped you!

  3. #3
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Is there a more efficient way of populating a VBA array

    Hi,

    Look at the Split Function in VBA.
    http://spreadsheetpage.com/index.php...plit_function/
    I think it is a lot easier using it than looping code.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  4. #4
    Valued Forum Contributor
    Join Date
    08-22-2011
    Location
    Auckland
    MS-Off Ver
    Excel 2019
    Posts
    716

    Re: Is there a more efficient way of populating a VBA array

    Thanks for the replies.
    As soon as I saw the loop I thought "why didn't I think of that" Thanks

    The split function looks interesting, it's something else I'll have to learn to use
    I'll leave the thread open for a little while, just in case
    Cheers

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Is there a more efficient way of populating a VBA array

    Where are the values for the array coming from?
    If posting code please use code tags, see here.

  6. #6
    Valued Forum Contributor
    Join Date
    08-22-2011
    Location
    Auckland
    MS-Off Ver
    Excel 2019
    Posts
    716

    Re: Is there a more efficient way of populating a VBA array

    Hi Norrie
    I've got 40 variables that hold a number value after the first part of the code has run & I've got the array loaded & can print the contents to a worksheet. But the way I've had to load the array seems horrible. I can't load the array with the loop & Split is over my head at the mo, I'm trying to get my head round it. I'm alright with the rest of it as it's a work in progress, it's the array part I'm trying to learn
    Thanks
    Please Login or Register  to view this content.

  7. #7
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Is there a more efficient way of populating a VBA array

    For example:
    Please Login or Register  to view this content.
    Note: don't use End on its own without a really good reason.
    Remember what the dormouse said
    Feed your head

  8. #8
    Valued Forum Contributor
    Join Date
    08-22-2011
    Location
    Auckland
    MS-Off Ver
    Excel 2019
    Posts
    716

    Re: Is there a more efficient way of populating a VBA array

    Thanks Romperstomper
    That made my code look amateurish lol. I had thought of Case 1 to 40, but as I hadn't tried that before I thought I'd stick to something that I know works.
    The array creates it's own variable via the case loop, that makes things a lot easier to understand.
    The reason I used End is when I first started to learn VBA, I had a few macros that ran right over the exit sub, but when I changed it to End, it stopped there. It was probably my even more amateurish code that was the real cause.
    I've learned something
    Thanks
    Phil

+ 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. 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
  2. [SOLVED] Make large chunk of If statements efficient. Array maybe?
    By mdovey in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 04-20-2014, 11:32 AM
  3. 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
  4. Most efficient way to find string in array members
    By davegugg in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-06-2010, 12:08 PM
  5. [SOLVED] Efficient Array Formula Construction
    By Mike Moore in forum Excel General
    Replies: 8
    Last Post: 07-23-2006, 07:37 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