+ Reply to Thread
Results 1 to 3 of 3

fast VBA copy to range?

  1. #1
    Valued Forum Contributor
    Join Date
    11-15-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2003
    Posts
    538

    fast VBA copy to range?

    I have
    dim buff(60000) as string
    and three thousand elements are used, each with exactly 80 bytes of data.

    What is the awesome way to plop that into range myRange and the 2,999 cells under it? Or the 59,999 cells under it, even if most are empty? IOW I want myRange to get buff(0), myRange.offset(1,0) to get buff(1), etc. By the way I don't want to hardcode the 3000 (though the 80 is okay).

    If I go Range("myRange").Resize(100, 1) = buff
    it loads the correct cells, but every single cell contains buff(0).
    (BTW I'm just arbitrarily using 100 for discussion)

    I can go Range("myRange").Resize(1, 100) = buff
    but that loads cells to the RIGHT of myRange, not below it. I know, duh, but at least it gets what I want; I still have to transpose, a PITA, and besides I'll run out of columns if there's enough input.

    Range("myRange") = buff
    just loads one cell, with buff(0)

    In case it matters, I can also have all of buff's contents in a variant or something else. I tried
    Dim buff2(1, 60000) As String
    Range("myRange").Resize(1, 100) = buff2
    which of course loads to the right;

    Range("myRange").Resize(100, 1) = buff2
    oddly picked up 1 good value, skipped a cell, then 98 #N/A values. Very weird.
    (Note: maybe I should have gone Dim buff2(1:1, 60000) As String?)

    You know, I could go something like
    Please Login or Register  to view this content.
    but I hoped I could do better with .resize or another direct assignment.
    Chip, if you can read this, you're STILL making the world a better place with your immeasurably valuable gifts. Every week, you continue to help me (and untold numbers of others) tremendously. Practically no alternative outlets can match your effectiveness, even over the vast number of topics you demystify and empower usage of. You were, and still are, amazing.

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

    Re: fast VBA copy to range?

    Try this...

    Range("myRange").Resize(UBound(buff)).Value = Application.Transpose(buff)

    Or this if buff is zero based
    Range("myRange").Resize(UBound(buff) + 1).Value = Application.Transpose(buff)
    Last edited by AlphaFrog; 06-06-2013 at 11:13 PM.

  3. #3
    Valued Forum Contributor
    Join Date
    11-15-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2003
    Posts
    538

    Re: fast VBA copy to range?

    Awesome! Right on the money!

    By the way, dim buff2(0 to 0,...) didn't succeed. I hoped that that would effectively accomplish the transpose, but it's plenty fast enough as you've stated. And I was unaware of that use of it as a method and having an argument. Way to go!

+ 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