+ Reply to Thread
Results 1 to 9 of 9

Paste array to range

Hybrid View

Bob@Sun Paste array to range 02-02-2011, 05:58 AM
DonkeyOte Re: Paste array to range 02-02-2011, 06:02 AM
Bob@Sun Re: Paste array to range 02-02-2011, 06:25 AM
DonkeyOte Re: Paste array to range 02-02-2011, 06:29 AM
Bob@Sun Re: Paste array to range 02-02-2011, 06:38 AM
DonkeyOte Re: Paste array to range 02-02-2011, 06:50 AM
snb Re: Paste array to range 02-02-2011, 06:55 AM
Bob@Sun Re: Paste array to range 02-02-2011, 06:56 AM
DonkeyOte Re: Paste array to range 02-02-2011, 07:32 AM
  1. #1
    Forum Contributor Bob@Sun's Avatar
    Join Date
    09-03-2009
    Location
    Montuak, Usa
    MS-Off Ver
    Excel 2007
    Posts
    438

    Paste array to range

    Hello,

    I have a dynamic array which I need to paste in excel

     a(1 To 7, 1 To i)

    How can I do that?

    Thanks,
    BK

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Paste array to range

    You can use UBound (& LBound where nec.)

    Range("A1").Resize(UBound(a,1)+1-LBound(a,1),UBound(a,2)+1-LBound(a,2)).Value = a
    If you know you will always use Base 1 then:

    Range("A1").Resize(UBound(a,1),UBound(a,2)).Value = a
    Last edited by DonkeyOte; 02-02-2011 at 06:35 AM. Reason: typo - last ref in first code should have been L rather than U

  3. #3
    Forum Contributor Bob@Sun's Avatar
    Join Date
    09-03-2009
    Location
    Montuak, Usa
    MS-Off Ver
    Excel 2007
    Posts
    438

    Re: Paste array to range

    The problem is that using Ubound gives me always 7 as the array is defined like this:

    a(1 to 7 , 1 to i)

  4. #4
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Paste array to range

    Not if you specify the dimension it doesn't (it defaults to dimension 1 if unspecified)

    Did you test the example ?

  5. #5
    Forum Contributor Bob@Sun's Avatar
    Join Date
    09-03-2009
    Location
    Montuak, Usa
    MS-Off Ver
    Excel 2007
    Posts
    438

    Re: Paste array to range

    I tested it but could not make it to work.

    Attached is a sample file. You can see the array how it is assigned, could you please paste it as I have shown in the excel?

    Thanks
    BK
    Attached Files Attached Files

  6. #6
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Paste array to range

    If you're trying to return the 7x2 array to 2x7 range then you will need to transpose the Array and invert the Ubounds, eg:

    Range("A2").Resize(UBound(a, 2), UBound(a, 1)).Value = Application.Transpose(a)
    I am presuming this is a hypothetical scenario given the way the Array is being populated...

    (and again, the above is assuming Option Base 1 at all times re: a)

  7. #7
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Paste array to range

    Sub Test()
    Dim a()
    
    ReDim a(1 To 7, 1 To 1)
    i = 1
    ReDim Preserve a(1 To 7, 1 To i)
    
    a(1, i) = "A-1"
    a(2, i) = "B-1"
    a(3, i) = "C-1"
    a(4, i) = "D-1"
    a(5, i) = "E-1"
    a(6, i) = "F-1"
    a(7, i) = "G-1"
    
    i = i + 1
    ReDim Preserve a(1 To 7, 1 To i)
    
    a(1, i) = "A-2"
    a(2, i) = "B-2"
    a(3, i) = "C-2"
    a(4, i) = "D-2"
    a(5, i) = "E-2"
    a(6, i) = "F-2"
    a(7, i) = "G-2"
    
    Cells(20, 1).Resize(7, i) = a
    
    End Sub



  8. #8
    Forum Contributor Bob@Sun's Avatar
    Join Date
    09-03-2009
    Location
    Montuak, Usa
    MS-Off Ver
    Excel 2007
    Posts
    438

    Re: Paste array to range

    Thanks a lot, this is working as expected now.

    One question though, what means that the code is assuming Option Base 1 at all times?

    Thanks again

    BK

  9. #9
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Paste array to range

    see VBE Help re: Option Base

+ 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