View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 27,285
Default accessing data from an array

This is what I meant:

Sub Tester3()
Dim varData() As Double
ReDim varData(1 To Range("Nums").Count, 1 To 1)

i = 0
For Each c In Range("nums")
i = i + 1
varData(i, 1) = c.Value
Next

Range("A1").Resize(i, 1).Value = varData
End Sub

I had a single typo, a "+" where there was supposed to be a ","

My apologies.

You shouldn't have changed varData(i+1) to varData(i+1,1), all you had to do
was replace the + with a comma which was the typo.

--
Regards,
Tom Ogilvy



Alan Beban wrote in message
...
varData is ReDimmed as a 2-d array, so varData(i+1) throws an error.

When that is changed to varData(i+1,1), the code throws an error at
varData(i+1,1) when i=the number of elements in Nums.

I think the following code does what Tom Ogilvy intended:

Dim varData() As Double
ReDim varData(1 To Range("Nums").Count, 1 To 1)

i = 1
For Each c In Range("Nums")
varData(i, 1) = c.Value
Debug.Print i, (varData(i, 1))
i = i + 1
Next

Range("D1").Resize(i - 1, 1).Value = varData '<--I used D1 'cause
I 'have something else in A1

But that raises the fundamental problem that all it does is create
another range that is identical to Nums; what's the point? I.e., if the
OP already has Nums, she already has the output she said she's seeking!??

Alan Beban

Tom Ogilvy wrote:
Assume Nums is a single column or single row

Dim varData() as double
Redim VarData(1 to Range("Nums").count, 1 to 1)

i = 0
For each c in Range("nums")
i = i + 1
varData (i+1) = c.Value
Next

Range("A1").Resize(i,1).Value = varData