Results 1 to 3 of 3

Thread: Setting Array = Excel Range Values?

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2004
    Posts
    43

    Question Setting Array = Excel Range Values?

    Is there a way to set the indices of an array equal to the values of cells in a range? Like this:

    Dim myArray(1 to 2, 1 to 5) As Long
    myArray = Range(Cells(1,1), Cells(2,5)).Value

    I can't get the above code to work. I can easily go the other way:

    Range(Cells(1,1), Cells(2,5)).Value = myArray

    Is there a trick to it, other than simply looping through each cell and storing the value in the corresponding array index? Any help would be appreciated.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Setting Array = Excel Range Values?

    No, no shortcut.
    Since your array is a long you may not want to depend on the default values
    for the cells. Explicitly code out the entire objects and then convert them to
    a long to match your array.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2004
    Posts
    43

    Re: Setting Array = Excel Range Values?

    The As Long declaration was arbitrary. The actual application in which I wish to use this concept contains only numerical values that will be treated as double precision.

    I just discovered that the following does work:

    Dim myArray as Variant
    Range(Cells(1, 1), Cells(2, 5)).Value = 123
    myArray = Range(Cells(1, 1), Cells(2, 5)).Value
    Range(Cells(3, 1), Cells(4, 5)).Value = myArray

    After executing the above code the values in the first range are rewritten into the second range. Unfortunately this doesn't work if I dimension the myArray variable as an array, even if the dimensions of the array exactly match the size of the range. I don't understand this behavior. Why would it work in one direction (dimensioned array to range) but not in the other (range to dimensioned array)? I guess it's just one of those things.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width