Click here to Skip to main content
15,913,773 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to copy data from a VB array into Excel. Using the method transfer data cell by cell, transferring 10,000 plus records was taking too long. There is a quicker way and that is transferring the array of data. I have the syntax etc but cannot seem to get it displayed on my Excel spreadsheet.

The array is like as follows

'Create an array with 3 columns and 4 rows
DataArray(0, 0) = “Accounts”
DataArray(1,0) = “Smith”
DataArray(2,0) = “John”
DataArray(0, 1) = “Accounts”
DataArray(1,1) = “Green”
DataArray(2,1) = “Jill”
DataArray(0, 2) = “Accounts”
DataArray(1,2) = “Brown”
DataArray(2,2) = “Jeff”
DataArray(0, 3) = “HR”
DataArray(1,3) = “Neil”
DataArray(2,3) = “Anne”


Code is as follows

'Add headers to the worksheet on row 1.
oSheet = oBook.Worksheets(1)
oSheet.Range("A1").Value = "Department"
oSheet.Range("B1").Value = "Surname"
oSheet.Range("C1").Value = "Forename"

'Transfer the array to the worksheet starting at cell A2.
oSheet.Range("A2").Resize(4, 3).Value = DataArray

The result I get in the Excel spreadsheet is

Department Surname Forename
Accounts Accounts Accounts etc

Any idea where I am going wrong?
Posted

Regarding this, which is pretty much the same as your current query (vb.net copy array to excel spreadsheet
[^]), you seem to be struggling with multi-dimensional arrays. Think of the indices of your multi-dimensiona array as row/column. So x(0,0) refers to the first row, first column; x(0,1) refers to the first row, second column, and x(1,0) starts the second row. So for the above data (and the same for your current query), the data is displaying correctly as you have it in the array. If you want it to display differently, load it into the array differently. Here's a simple loop to demonstrate--select whatever positive number you want as NumRows and NumColumns:

VB
Dim MyArray(NumRows - 1, NumColumns - 1) as String
For row as Integer = 0 to NumRows - 1
    For column as Integer = 0 to NumColumns - 1
        MyArray(row, col) = row & "_" & column
    Next column
Next row
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900