View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Jim Cone[_2_] Jim Cone[_2_] is offline
external usenet poster
 
Posts: 1,549
Default Read and write an array


Be careful what you wish for. <g
'--

Sub OneByOneWithRanges()
Dim rngFirst As Range
Dim rngOther As Range
Dim N As Long

Set rngFirst = Range("A1:A4").Cells
Set rngOther = Range("C10:F10").Cells

For N = 1 To rngFirst.Count
rngOther(N).Value = rngFirst(N).Value
Next
End Sub
'--

Sub OneByOneWithArrayAndRange()
Dim vFirst As Variant
Dim rngOther As Range
Dim N As Long

'A variant containing an array.
vFirst = Range("A1:A4").Value
Set rngOther = Range("C10:F10").Cells

'Transfer array values to other range
For N = 1 To UBound(vFirst)
rngOther(N).Value = vFirst(N, 1)
Next
End Sub
'--

Sub UseTwoArrays()
Dim vFirst As Variant
Dim vOther() As Variant
Dim M As Long
Dim N As Long
Dim i As Long
Dim j As Long

vFirst = Range("A1:A4").Value
i = UBound(vFirst, 1)
j = UBound(vFirst, 2)

' Transpose array size - columns to rows and rows to columns.
ReDim vOther(1 To j, 1 To i)

' Add values from 1st array to 2nd array
' Note (N, M) vs. (M, N)
For M = 1 To i
For N = 1 To j
vOther(N, M) = vFirst(M, N)
Next
Next

' Add 2nd array values to range.
Range("C10:F10").Value = vOther()
End Sub
--
Jim Cone
Portland, Oregon USA




"Dave L"
wrote in message
Thanks Jim. That works for some of the simpler things I need to do, but I was
hoping to use a loop to grab each cells' value separately and then write it
separately. In other words I want to select A1 and read the value into the
array, select A2 and read the value into the array, etc. Then I want to move
to B1 and write the first value, B2 and write the second, etc.

I could explain the whole scope of the project, but there will be way too
many variations on the scenario above. I'm really just looking for the basic
concept of a for next loop and then I can take it from there.