Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 7
Default How to write an array to a range without looping

Hello,
I have an array MyArray(10000,3), and I want to write this into sheet
1 starting at row 1 col 1. I would like to do it without using a for
loop or a counter. I am looking for a way to paste the entire array
at once. The reason for this is processing speed. A for loop takes a
while when the array gets large.

thanks,
Andy
  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 587
Default How to write an array to a range without looping

hi Andy,

Sub Test()
Dim a(1 To 3, 1 To 2)

For i = 1 To 3
a(i, 1) = i
a(i, 2) = i * 3
Next i

[A1:B3] = a
End Sub


--
isabelle

Le 2011-04-26 08:18, Andy a écrit :
Hello,
I have an array MyArray(10000,3), and I want to write this into sheet
1 starting at row 1 col 1. I would like to do it without using a for
loop or a counter. I am looking for a way to paste the entire array
at once. The reason for this is processing speed. A for loop takes a
while when the array gets large.

thanks,
Andy

  #3   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 2,059
Default How to write an array to a range without looping

On Apr 26, 5:18*am, Andy wrote:
I have an array MyArray(10000,3), and I want to write this
into sheet 1 starting at row 1 col 1. I would like to do
it without using a for loop or a counter.


Ostensibly, simply:

Sheets("sheet1").Range("a1:c10000") = MyArray

or

Sheets("sheet1").Range("a1").resize(10000,3) = MyArray

But if you declare the array as Dim MyArray(10000,3) with the default
Option Base (0), your array indices run from 0 to 10000 and 0 to 3,
even if you use only 1 to 10000 and 1 to 3.

Consequenty, the assignment statements above will shift and truncate
your data by one row and one column.

Although you could set Option Base 1, I prefer to declare the range of
indices explicitly, e.g. Dim MyArray(1 to 10000,1 to 3).

Then the assignment statements above will have the intended result.
  #4   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 420
Default How to write an array to a range without looping

I would use something like this:

Option Explicit
Sub testme()
Dim myArr(1 To 5, 1 To 3) As Long
Dim rCtr As Long
Dim cCtr As Long
Dim DestCell As Range

'test data only
For rCtr = LBound(myArr, 1) To UBound(myArr, 1)
For cCtr = LBound(myArr, 2) To UBound(myArr, 2)
myArr(rCtr, cCtr) = rCtr * cCtr
Next cCtr
Next rCtr

Set DestCell = Worksheets("Sheet1").Range("a1")

DestCell.Resize(UBound(myArr, 1) - LBound(myArr, 1) + 1, _
UBound(myArr, 2) - LBound(myArr, 2) + 1).Value _
= myArr

End Sub



On 04/26/2011 07:18, Andy wrote:
Hello,
I have an array MyArray(10000,3), and I want to write this into sheet
1 starting at row 1 col 1. I would like to do it without using a for
loop or a counter. I am looking for a way to paste the entire array
at once. The reason for this is processing speed. A for loop takes a
while when the array gets large.

thanks,
Andy


--
Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I write an array to include all worksheets in a workbook? Jodie Excel Worksheet Functions 8 October 13th 09 04:06 PM
How to write (Selected range - 1) Damian Excel Discussion (Misc queries) 2 September 4th 09 08:20 PM
How do I write formula to check a range of cells? Tayo Excel Worksheet Functions 5 August 27th 09 05:53 PM
Looping thru a range of cells COBOL Dinosaur New Users to Excel 9 June 2nd 07 03:41 AM
looping across columns in range? Amy Excel Discussion (Misc queries) 3 July 19th 05 08:01 PM


All times are GMT +1. The time now is 01:25 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"