+ Reply to Thread
Results 1 to 3 of 3

Paste Multidimensional Array into 1 Range Column

Hybrid View

  1. #1
    Registered User
    Join Date
    08-16-2007
    Posts
    50

    Paste Multidimensional Array into 1 Range Column

    Hello,
    I am trying to create a macro that will take a multidimensional array and paste each column of the array into one column of a worksheet. I figured out how to create the array, and I can paste the array into a range with same dimensions. I have been looking for a way to paste one column at a time below each other to get the entire content of the array into one column. Does anyone know how to paste one entire column of a multidimensional array into a range? Or, does anyone have a better way of accomplishing the concept? Below is the test code I have been using. Any help would be appreciated.
    thank you

    Sub ArrayTest()
    Dim NumOfColumns As Integer, NumOfRows As Integer
    Dim TestArray() As String
    
    NumOfColumns = 3
    NumOfRows = 3
    
    ReDim Preserve TestArray(NumOfRows, NumOfColumns - 1) As String
        TestArray(0, 0) = "A"
        TestArray(0, 1) = "B"
        TestArray(0, 2) = "C"
        TestArray(1, 0) = "1"
        TestArray(2, 0) = "2"
        TestArray(3, 0) = "3"
        TestArray(1, 1) = "1"
        TestArray(1, 2) = "1"
    
    'This doesn't work, but I would like something equivalent to:
    'Range("d1:d4").Value = TestArray.Column(1)
    'Range("d5:d6").Value = TestArray.Column(2)
    'Range("d7:d8").Value = TestArray.Column(3)
    
    End Sub
    Last edited by mcclanat; 05-07-2012 at 07:55 PM.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Paste Multidimensional Array into 1 Range Column

    Option Explicit
    
    Sub ArrayTest()
        Dim nCol As Integer
        Dim nRow As Integer
        Dim TestArray() As String
    
        nCol = 3
        nRow = 3
    
        ReDim TestArray(1 To nRow, 1 To nCol)
        TestArray(1, 1) = "11"
        TestArray(1, 2) = "12"
        TestArray(1, 3) = "13"
        TestArray(2, 1) = "21"
        TestArray(2, 2) = "22"
        TestArray(2, 3) = "23"
        TestArray(3, 1) = "31"
        TestArray(3, 2) = "32"
        TestArray(3, 3) = "33"
        
        Range("D1:D4").Value = WorksheetFunction.Index(TestArray, 0, 1)
        Range("D5:D6").Value = WorksheetFunction.Index(TestArray, 0, 2)
        Range("D7:D8").Value = WorksheetFunction.Index(TestArray, 0, 3)
    End Sub
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    08-16-2007
    Posts
    50

    Re: Paste Multidimensional Array into 1 Range Column

    That worked. Thank you!

+ 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