+ Reply to Thread
Results 1 to 17 of 17

Using array variable to populate range

  1. #1
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,444

    Using array variable to populate range

    Hi all,

    I'm trying to populate a worksheet using arrays.

    Please Login or Register  to view this content.
    However, the output on the sheet suggests that icount has been incremented in steps of 2.

    eg

    Please Login or Register  to view this content.
    Any ideas? Anyone?

    Thanks,

    Dave
    Last edited by sweep; 12-18-2008 at 09:09 AM. Reason: Solved

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Where do you set a value to icount?
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,444
    Thanks for the response Roy,

    the rws variable in the loop is defined here:

    Please Login or Register  to view this content.

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    What about icount in the formula?

  5. #5
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,444
    Sorry if I'm having a "thick" day, but is it not set here?

    Please Login or Register  to view this content.

  6. #6
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243
    A long day eh Roy? ;-)
    The Icount is being set in the For statement "For iCount = 1 To rws".

    Rob
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

  7. #7
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    I didn';t notice that, recovering from the flu (LOL)

  8. #8
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,444
    Thank goodness for that - I thought I was being dafter than usual!

  9. #9
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606
    It's a mystery to me, but if you're not wedded to your current approach this seems to work:
    Please Login or Register  to view this content.
    EDIT: on reflection the double increment may be due to the fact that Excel automatically adjusts formulae across a range, added to which you are incrementing in the loop.

  10. #10
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    I was just trying without the array * it seemed to work
    Please Login or Register  to view this content.

  11. #11
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,444
    Thanks all,

    I'm using the array approach because it's quicker. There's over 45000 cells to populate, and using a "normal" loop to write to the sheet every time takes 3-4 minutes.

  12. #12
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,444
    Any ideas on a workaround?

  13. #13
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573
    Arrays are easily written to a range using the Range's Resize property.

    Whether you use an array method or another, I would recommend that you turn off calculation, screen updates and events as I did in http://vbaexpress.com/kb/getarticle.php?kb_id=1035

  14. #14
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,444
    Thanks Kenneth,

    I have events, calculations and screenupdate turned off.

    Could you further explain the resize method wrt arrays?

  15. #15
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,444
    Sorted it out. Quickest method I could find was using range.formula for the first line and then range.autofill for the additional.

    Thanks all.

  16. #16
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Does this work?

    Please Login or Register  to view this content.

  17. #17
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,444
    Hi Roy,

    Thanks, but I still get a double increment in iCount in the spreadsheet. Not to worry - I'll make do with the autofill method.

    Thanks to all,

    Dave

+ 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