Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 25
Default write array to range

Hi all,

I'm picking up Excel VBA again and have some trouble with this:

Range("b2", "b501") = NPV,

where NPV is an array and it looks OK, but the output is all the same,
500 of the first element, what's wrong?

Thank you.

  #2   Report Post  
Posted to microsoft.public.excel.programming
Ed Ed is offline
external usenet poster
 
Posts: 65
Default write array to range

I'm not a great expert, so someone who is may have a better answer.
But I think you have to identify which element of the array you want to
write. If I had something like this, I'd probably do something like:

Dim x As Long
For x = 0 To UBound(NPV)
Range("B" & x+2) = NPV(x)
Next x

(This assumes you did not set Option Base 1 to start your array count
at 1, and it is set as default to write the first term to NPV(0). If
you did start at 1, then use
For x = 1 To UBound(NPV)
Range("B" & x+1) = NPV(x)

Ed

muster wrote:
Hi all,

I'm picking up Excel VBA again and have some trouble with this:

Range("b2", "b501") = NPV,

where NPV is an array and it looks OK, but the output is all the same,
500 of the first element, what's wrong?

Thank you.


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 25
Default write array to range

Thank you Ed. Your way surely works. The way I asked suppose to be a
faster way to write an array to a range, that is to say, if it works.

Thank you and more hints please.


Ed wrote:
I'm not a great expert, so someone who is may have a better answer.
But I think you have to identify which element of the array you want to
write. If I had something like this, I'd probably do something like:

Dim x As Long
For x = 0 To UBound(NPV)
Range("B" & x+2) = NPV(x)
Next x

(This assumes you did not set Option Base 1 to start your array count
at 1, and it is set as default to write the first term to NPV(0). If
you did start at 1, then use
For x = 1 To UBound(NPV)
Range("B" & x+1) = NPV(x)

Ed

muster wrote:
Hi all,

I'm picking up Excel VBA again and have some trouble with this:

Range("b2", "b501") = NPV,

where NPV is an array and it looks OK, but the output is all the same,
500 of the first element, what's wrong?

Thank you.


  #4   Report Post  
Posted to microsoft.public.excel.programming
Ed Ed is offline
external usenet poster
 
Posts: 65
Default write array to range

muster wrote:
Thank you Ed. Your way surely works.


I'm glad I could help. It feels good to finally be on the giving end.

The way I asked suppose to be a
faster way to write an array to a range, that is to say, if it works.


I'm not familiar enough to know all the faster ways to do things. I'm
still doing a lot of cave-man stuff myself. But it doesn't seem likely
that you can match a collection of data (which is what an array is) to
a range of cells - even though the range is made up of many cells, the
range itself is one single block. Maybe it can be done, but it strikes
me as like throwing a deck of cards at the box.

more hints please.


I wish I had more! <g My own code would probably look better!

Ed



Ed wrote:
I'm not a great expert, so someone who is may have a better answer.
But I think you have to identify which element of the array you want to
write. If I had something like this, I'd probably do something like:

Dim x As Long
For x = 0 To UBound(NPV)
Range("B" & x+2) = NPV(x)
Next x

(This assumes you did not set Option Base 1 to start your array count
at 1, and it is set as default to write the first term to NPV(0). If
you did start at 1, then use
For x = 1 To UBound(NPV)
Range("B" & x+1) = NPV(x)

Ed

muster wrote:
Hi all,

I'm picking up Excel VBA again and have some trouble with this:

Range("b2", "b501") = NPV,

where NPV is an array and it looks OK, but the output is all the same,
500 of the first element, what's wrong?

Thank you.


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default write array to range

Hi Muster,

Try:

Range("B2").Resize(UBound(NPV) - LBound(NPV) + 1).Value _
= Application.Transpose(NPV)

---
Regards,
Norman


"muster" wrote in message
oups.com...
Hi all,

I'm picking up Excel VBA again and have some trouble with this:

Range("b2", "b501") = NPV,

where NPV is an array and it looks OK, but the output is all the same,
500 of the first element, what's wrong?

Thank you.





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default write array to range

Hi Muster,

Just to add:

'=============
Public Sub TesterD01()
Dim NPV(1 To 500)
Dim i As Long

For i = 1 To 500
NPV(i) = i
Next i

Range("B2").Resize(UBound(NPV) - LBound(NPV) + 1).Value _
= Application.Transpose(NPV)
End Sub
'<<=============


---
Regards,
Norman



"Norman Jones" wrote in message
...
Hi Muster,

Try:

Range("B2").Resize(UBound(NPV) - LBound(NPV) + 1).Value _
= Application.Transpose(NPV)

---
Regards,
Norman



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 to write an array to a range without looping Andy Excel Worksheet Functions 3 April 27th 11 12:52 PM
How do I write an array to include all worksheets in a workbook? Jodie Excel Worksheet Functions 8 October 13th 09 04:06 PM
Write 1st and 2nd dimension array values to cells [email protected] Excel Programming 2 February 28th 06 11:17 PM
write 1-dimensional array of integers to text file RB Smissaert Excel Programming 3 October 12th 03 05:56 PM
Write to Array from other closed Excel file Dave B[_4_] Excel Programming 5 October 1st 03 04:48 PM


All times are GMT +1. The time now is 04:41 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"