PC Review


Reply
Thread Tools Rate Thread

How to assign an array to a range in Excel macro?

 
 
yunlai yang
Guest
Posts: n/a
 
      15th Jul 2003
Hi,

I tried to assign an array ro a range in Excel macro, but it does not work.

Here is the example

sub fill_range()
dim myArray(1 to 5) as single
dim i as integer
for i=1 to 5
myArray(i)=i
next

sheets("sheet1").activate
range("a1:a5")=myArray
end sub

When the macro is run, the values of cells a1 to a5 are 1 instead of 1 to
5. That is all the cells in the range took the value of myArray(1) instead
correspondent (right) ones.

Any suggestions on solving this problem?

Thanks

My e-mail address:
(E-Mail Removed). or
(E-Mail Removed)







 
Reply With Quote
 
 
 
 
yunlai yang
Guest
Posts: n/a
 
      15th Jul 2003
Hi, John

The line was not correct,as you said. Because the format of the array is
different from that of the range. I redeclared, according to suggestions
from Dick (see his advice) the array as myArray(5, 1). Now it works.

Yes, you can use loop to assign an array to a range. THis is normally OK, if
you array is not big. If it is big, it will take a long time for the reason
that there are same number of interface change between excel sheet and your
program. Using a single statement will only have one interface exchange and
same you a lot of time and frustrations.

Yunlai


"John" <(E-Mail Removed)> wrote in message
news:mjensen-(E-Mail Removed)...
>
> Yunlai,
> The line "range("a1:a5")=myArray" is incorrect. Since you do not tell it
> which element(s) of myArray go to the range, the default is the first
> element, which is "1". I don't know if you can assign the whole array at
> once (I've never tried it), but I would use a loop in Excel to load each
> element of myArray into the range.
>
> John



 
Reply With Quote
 
John Ford
Guest
Posts: n/a
 
      15th Aug 2003
Yunlai,

I don't see a post from Dick. What was his advice?

--
jcf

<yunlai yang> wrote in message news:bf1lb7$ggt$(E-Mail Removed)...
| Hi, John
|
| The line was not correct,as you said. Because the format of the array is
| different from that of the range. I redeclared, according to suggestions
| from Dick (see his advice) the array as myArray(5, 1). Now it works.
|
| Yes, you can use loop to assign an array to a range. THis is normally OK, if
| you array is not big. If it is big, it will take a long time for the reason
| that there are same number of interface change between excel sheet and your
| program. Using a single statement will only have one interface exchange and
| same you a lot of time and frustrations.
|
| Yunlai
|
|
| "John" <(E-Mail Removed)> wrote in message
| news:mjensen-(E-Mail Removed)...
| >
| > Yunlai,
| > The line "range("a1:a5")=myArray" is incorrect. Since you do not tell it
| > which element(s) of myArray go to the range, the default is the first
| > element, which is "1". I don't know if you can assign the whole array at
| > once (I've never tried it), but I would use a loop in Excel to load each
| > element of myArray into the range.
| >
| > John
|
|
 
Reply With Quote
 
John Ford
Guest
Posts: n/a
 
      15th Aug 2003
Never mind... I saw Dick's response on ms.public.office.developer.vba. I didn't notice the cross-post!

"John Ford" <(E-Mail Removed)> wrote in message news:#(E-Mail Removed)...
| Yunlai,
|
| I don't see a post from Dick. What was his advice?
|
| --
| jcf
|
| <yunlai yang> wrote in message news:bf1lb7$ggt$(E-Mail Removed)...
| | Hi, John
| |
| | The line was not correct,as you said. Because the format of the array is
| | different from that of the range. I redeclared, according to suggestions
| | from Dick (see his advice) the array as myArray(5, 1). Now it works.
| |
| | Yes, you can use loop to assign an array to a range. THis is normally OK, if
| | you array is not big. If it is big, it will take a long time for the reason
| | that there are same number of interface change between excel sheet and your
| | program. Using a single statement will only have one interface exchange and
| | same you a lot of time and frustrations.
| |
| | Yunlai
| |
| |
| | "John" <(E-Mail Removed)> wrote in message
| | news:mjensen-(E-Mail Removed)...
| | >
| | > Yunlai,
| | > The line "range("a1:a5")=myArray" is incorrect. Since you do not tell it
| | > which element(s) of myArray go to the range, the default is the first
| | > element, which is "1". I don't know if you can assign the whole array at
| | > once (I've never tried it), but I would use a loop in Excel to load each
| | > element of myArray into the range.
| | >
| | > John
| |
| |
 
Reply With Quote
 
New Member
Join Date: Oct 2010
Posts: 1
 
      15th Oct 2010
This will work

Sub test()
Dim strA(5) As String

strA(0) = "A"
strA(1) = "B"
strA(2) = "C"
strA(3) = "D"
strA(4) = "E"

Cells(1, 1).Offset(0, 0).Resize(1, UBound(strA)) = strA
End Sub

I remember seen this solution in a book long back; thought of posting this if in case helps to others

Last edited by bbcalagu; 15th Oct 2010 at 06:43 AM..
 
Reply With Quote
 
New Member
Join Date: Nov 2010
Posts: 1
 
      10th Nov 2010
You're original post was almost correct.

to use one line to dump an array to a range or vice versa, make sure to use the .value property of the range.

For example:

Use myArray=Range("A1:Z26").Value
Not myArray=Range("A1:Z26")

The same goes for the reverse
Correct
Range("A1:B12").Value=myArray

Incorrect:
Range("A1:B12")=myArray
 
Reply With Quote
 
New Member
Join Date: Jan 2012
Posts: 1
 
      30th Jan 2012
Copying an Array to a Worksheet Range without looping:

From what I read in the post, yes Dick's solution worked.

Array declaration must be atleast 2-Dim - indicates (Row, Col) ??
If you intend to copy to a multiple column range, you'll need to increment the 2nd dimension.

These work:

Option Base 1
'Single Column Range
Sub Test1()
Dim myArray(5, 1) As Double
Dim i As Integer
For i = 1 To 5
myArray(i, 1) = i / 3
Next
Sheets("sheet1").Activate
Range("a1:a5") = myArray
End Sub


'Multiple Column range
Sub Test2()
Dim myArray(5, 2) As Double
Dim i As Integer
For i = 1 To 5
myArray(i, 1) = i / 3
myArray(i, 2) = i / 10
Next
Sheets("sheet1").Activate
Range("a1:b5") = myArray
End Sub
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to assign range to Double array? Joe User Microsoft Excel Programming 0 23rd Nov 2009 06:45 PM
Assign Range passed to fuction to an array Michael Microsoft Excel Programming 4 3rd Mar 2009 07:52 PM
Assign a 2D array of constant values to a range dodgo Microsoft Excel Programming 4 19th Oct 2006 04:46 AM
Excel 2003. Assign array to range Jorge Vinuales Microsoft Excel Programming 2 2nd May 2006 06:06 PM
Assign char range/set to array? John E. Microsoft C# .NET 1 7th Jan 2005 04:35 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:44 PM.