+ Reply to Thread
Results 1 to 10 of 10

Transfer Array data to Spreadsheet

Hybrid View

  1. #1
    Registered User
    Join Date
    06-10-2008
    Posts
    14

    Transfer Array data to Spreadsheet

    Hi Guys,

    I'm trying to transfer information I have held in a table format into a database format.

    I'm using array variables to hold the table data and now I'm trying to use this data held within the array to produce a database.

    For example:

    A 1 2
    B 2 3
    C 3 4

    into

    A1
    A2
    B2
    B3
    C3
    C4

    My code would look like this:
    MyArray = range("A1:C3")
    Where MyArray(1,1) = "A" and MyArray(3,3) = "4"

    What I want to do is refer to one "column" of data within the array so that it will return for column A (or array position 1):

    A
    B
    C

    I can use a loop to do this for me but I was wondering whether there was a solution which would be a little neater?

    This doesn't work but something like:

    MyArray(1,1) to MyArray(1,3)
    Thanks for any help.

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

    Re: Transfer Array data to Spreadsheet

    [removed - misinterpeted, Kris has posted a solution specific to the question]
    Last edited by DonkeyOte; 08-05-2009 at 06:32 AM.

  3. #3
    Forum Contributor
    Join Date
    02-19-2005
    Location
    Gurgaon,India
    MS-Off Ver
    2007,2010,2013
    Posts
    180

    Re: Transfer Array data to Spreadsheet

    Sub kTest()
    Dim MyArray, MyNewArray()
    
    MyArray = [a1:c3]
    
    ReDim MyNewArray(1 To UBound(MyArray, 1) * UBound(MyArray, 2), 1 To 1)
    
    'before
    
    '/ A 1 2
    '/ B 2 3
    '/ C 3 4
    
    'after
    
    '/ A1
    '/ A2
    '/ B2
    '/ B3
    '/ C3
    '/ C4
    
    For i = 1 To UBound(MyArray, 1)
        For j = 2 To UBound(MyArray, 2)
            n = n + 1
            MyNewArray(n, 1) = MyArray(i, 1) & MyArray(i, j)
        Next
    Next
    [d1].Resize(n).Value = MyNewArray
    End Sub
    Kris

  4. #4
    Registered User
    Join Date
    06-10-2008
    Posts
    14

    Re: Transfer Array data to Spreadsheet

    Thank you for the response.
    The index gives me the number of "rows" in the array.

    My actual code looks like this:

    SkillArray = Range(Cells(HoursSR, FcstStartColMth), Cells(HoursFR, FcstEndCol))
    This now holds all of the range in the array.
    I now want to go to another spreadsheet and just off load "columns" of the data into the worksheet.

    for count LBound(SkillArray,1) to UBound(SkillArray,1)
    [a1].select
    [a1] = SkillArray(Count,1)
    activecell.offset(1,0)
    next count
    This will now unload the data in the array which relates to "Column" 1 in the source data to column A in a fresh spreadsheet.

    What I want to see is if I can unload this data without using this loop above in order to save run time as this is taking a long time with the amount of data that I have.

    I thought you might be able to get the array to dump all data relating to a certain dimension into a spreadsheet?

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

    Re: Transfer Array data to Spreadsheet

    I don't really understand what you want to do and if in fact if Kris' approach (which does what you initially requested) is what you want or not ?

    The INDEX I suggested was based on the fact that I had no clue as to what you were trying to achieve, I merely encased within the COUNTA to show that you could work with all values from one vector in that manner, eg:

    Dim MyArray: MyArray = Sheets("Sheet1").Range("A1:C3")
    Sheets("Sheet2").Range("A1").Resize(UBound(MyArray,1)).Value = Application.Index(MyArray,0,2)
    would return the second column of array to A1:Ax on Sheet2 etc... but I'm not sure this is what you want to do ...

  6. #6
    Forum Contributor
    Join Date
    02-23-2006
    Location
    Near London, England
    MS-Off Ver
    Office 2003
    Posts
    770

    Re: Transfer Array data to Spreadsheet

    Do you mean something like this?

    This code shows how you can extract a complete column or row of data from an Array onto a worksheet.

    Sub test()
    
    Dim MyArray
    
        'quick method to create an array to test with
        MyArray = [A1:C4]
        
        'Extract row 1 of the array into a row of cells starting at E1
        [E1].Resize(1, UBound(MyArray, 2) - LBound(MyArray, 2) + 1) = WorksheetFunction.Index(MyArray, 1, 0)
    
        'Extract column 2 of the array into a column of cells starting at E3
        [E3].Resize(UBound(MyArray, 1) - LBound(MyArray, 1) + 1, 1) = WorksheetFunction.Index(MyArray, 0, 2)
        
    End Sub
    Test Data in cells A1:C4 (avoided using letters and numbers as test data as that just confuses the issue! )
    Apple	Fruit	Green
    Beef	Meat	Brown
    Cheese	Dairy	Yellow
    Bread	Bakery	White
    If you find the response helpful please click the scales in the blue bar above and rate it
    If you don't like the response, don't bother with the scales, they are not for you

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

    Re: Transfer Array data to Spreadsheet

    Guys, just for ref. (given prevalence in this thread) it is my understanding that

    [A1]
    is slower than

    Range("A1") / Cells(1,1) / Cells(1,"A") etc...

  8. #8
    Registered User
    Join Date
    06-10-2008
    Posts
    14

    Re: Transfer Array data to Spreadsheet

    Yes, that is the concept. I just need to digest it and use it in my code!

    Is it possible to use 'Cells' instead of [range].resize?

    e.g. [a1].resize to cells(1,1).resize

    Thank you for your help!

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

    Re: Transfer Array data to Spreadsheet

    Yes, see syntax in post prior to Phil_V

  10. #10
    Forum Contributor
    Join Date
    02-23-2006
    Location
    Near London, England
    MS-Off Ver
    Office 2003
    Posts
    770

    Re: Transfer Array data to Spreadsheet

    Quote Originally Posted by wjones View Post
    Yes, that is the concept. I just need to digest it and use it in my code!

    Is it possible to use 'Cells' instead of [range].resize?

    e.g. [a1].resize to cells(1,1).resize

    Thank you for your help!
    YES

    In fact I would normally use "cells(1,1)", but decided to use the [a1] format as that was what you seemed to have used

    The code altered would be:
    Sub test()
    
    Dim MyArray
    
        'quick method to create an array to test with
        MyArray = Range(Cells(1,1), Cells(4,3))
        
        'Extract row 1 of the array into a row of cells starting at E1
        Cells(1,5).Resize(1, UBound(MyArray, 2) - LBound(MyArray, 2) + 1) = WorksheetFunction.Index(MyArray, 1, 0)
    
        'Extract column 2 of the array into a column of cells starting at E3
        Cells(3,5).Resize(UBound(MyArray, 1) - LBound(MyArray, 1) + 1, 1) = WorksheetFunction.Index(MyArray, 0, 2)
        
    End Sub

+ 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