View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Office_Novice Office_Novice is offline
external usenet poster
 
Posts: 245
Default Error trying to write array to range

try


Sub Cap()
Dim Rng As Range
Dim Arr(1 To 5)
Set Rng = Range("A2", Cells(2, 5))
Arr(1) = "AB"
Arr(2) = "CD"
Arr(3) = "EF"
Arr(4) = "GH"
Arr(5) = "IJ"
Rng.Value = Arr
End Sub

"J. Caplan" wrote:

I have data in a two dimensional array of Variants but I get an
"Application-defined or Object-defined error" (number 1004) when I try to
assign to the array to the range. I tried to simplify this with a
one-dimensional array of strings and that still fails. Here is my simple
test below

Public Sub MyTest()

Dim arr() As String
ReDim arr(1 To 5)

On Error GoTo ErrorHandler

arr(1) = "AB"
arr(2) = "CD"
arr(3) = "EF"
arr(4) = "GH"
arr(5) = "IJ"

Dim rng As Range
Set rng = Range("A2", Cells(2, 5))
Debug.Print (rng.Address) ' <== Returns $A$2:$E$2

rng.value = arr ' <== ERROR OCCURS ON THIS LINE

ErrorHandler:
Call MsgBox(Err.Description & " - " & Err.Number)
End Sub

What am I missing?