PDA

View Full Version : Transfer Array to worksheet



yn19832
03-23-2007, 07:40 AM
I want to transfer the array "varArray" to the shee1 with the following codes, but it turned out to be a mass. The data in "tblIndex" is like this

Date Country Type Index
31/03/1980 Canada A 1
... ... ... ...

Set rec = db.OpenRecordset("tblIndex", dbOpenDynaset)

If Not rec.EOF Then
rec.MoveLast
rec.MoveFirst
intRecord = rec.RecordCount
varArray = rec.GetRows(intRecord)
intFieldCount = UBound(varArray, 1)
intRowCount = UBound(varArray, 2)

' Make Sure Sheet1 is Activate
Sheets("Sheet1").Activate

' Set worksheet range
Set TheRange = ActiveCell.Range(Cells(1, 1), Cells(intRowCount + 1, intFieldCount + 1))

' Copy the record to Excel
TheRange.Value = varArray

Bob Phillips
03-23-2007, 07:57 AM
intFieldCount = UBound(varArray, 2)
intRowCount = UBound(varArray, 1)
' Make Sure Sheet1 is Activate
Sheets("Sheet1").Activate
' Set worksheet range
Set TheRange = ActiveCell.Range(Cells(1, 1), Cells(intRowCount, intFieldCount))
' Copy the record to Excel
TheRange.Value = varArray

yn19832
03-23-2007, 08:19 AM
Many thanks,I tried but it transferd to Excel like this, without the last field and turned around.


31/01/1980 29/02/198 0 31/03/1980
Canada Canada Canada
Sentiment Sentiment Sentiment

But it should be
31/01/1980 Canada Sentiment 12
29/02/1980 Canada Sentiment 45
31/03/1980 Canada Sentiment 34

Bob Phillips
03-23-2007, 10:46 AM
For the orientation, just change intFieldCount and intRowCount around. The rows/columns should be right though, you are just picking up the array dimensions, but if base 0, you have a problem so add 1 back in. Which brings you back to where you started, so what was wrong with your code?