+ Reply to Thread
Results 1 to 17 of 17

Using array variable to populate range

Hybrid View

  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,454

    Using array variable to populate range

    Hi all,

    I'm trying to populate a worksheet using arrays.

    
    ReDim TempArray(1 To rws) 'set the size of the array
    Set TheRange = Range(Range("A2"), Range("A" & rws)) 'define the destination range
    For iCount = 1 To rws
    TempArray(iCount) = "=IF('Raw Data'!R[58]C="""","""",'Raw Data'!R[" & 57 + iCount & "]C)" 'fill the array
    Next iCount
    TheRange.FormulaR1C1 = TempArray 'transfer the array to the sheet
    However, the output on the sheet suggests that icount has been incremented in steps of 2.

    eg

    =IF('Raw Data'!A60="","",'Raw Data'!A60)
    =IF('Raw Data'!A62="","",'Raw Data'!A62)
    =IF('Raw Data'!A64="","",'Raw Data'!A64)
    =IF('Raw Data'!A66="","",'Raw Data'!A66)
    =IF('Raw Data'!A68="","",'Raw Data'!A68)
    =IF('Raw Data'!A70="","",'Raw Data'!A70)
    =IF('Raw Data'!A72="","",'Raw Data'!A72)
    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,454
    Thanks for the response Roy,

    the rws variable in the loop is defined here:

    Sheets("Raw Data").Activate
    rws = Range(Range("A1"), Range("A1").End(xlDown)).Rows.count

  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,454
    Sorry if I'm having a "thick" day, but is it not set here?

    ReDim TempArray(1 To rws) 'set the size of the array
    Set TheRange = Range(Range("A2"), Range("A" & rws)) 'define the destination range
    For iCount = 1 To rws
    TempArray(iCount) = "=IF('Raw Data'!R[58]C="""","""",'Raw Data'!R[" & 57 + iCount & "]C)" 'fill the array
    Next iCount
    TheRange.FormulaR1C1 = TempArray 'transfer the array to the sheet

  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,454
    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:
    Set TheRange = Range(Range("A2"), Range("A" & rws)) 'define the destination range
    TheRange.FormulaR1C1 = "=IF('Raw Data'!R[58]C="""","""",'Raw Data'!R[" & 57 + iCount & "]C)"
    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
    Sheets("Raw Data").Activate
    rws = Range(Range("A1"), Range("A1").End(xlDown)).Rows.Count
    Set TheRange = Range(Range("A2"), Range("A" & rws)) 'define the destination range
    For icount = 1 To rws
    Cells(icount, 1).Formula = "=IF('Raw Data'!R[58]C="""","""",'Raw Data'!R[" & 57 + icount & "]C)" 'fill the array
    Next icount

  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,454
    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,454
    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,454
    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,454
    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?

    With Sheets("Raw Data")
    rws = .Range(.Range("A1"), .Range("A1").End(xlDown)).Rows.Count
    Set therange = .Range(.Range("A2"), .Range("A" & rws)) 'define the destination range
    therange.Formula = "=IF('Raw Data'!R[58]C="""","""",'Raw Data'!R[" & 57 + icount & "]C)" 'fill the array
    End With

  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,454
    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