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

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?

  #2   Report Post  
Posted to microsoft.public.excel.programming
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?

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

Thanks, but that did not make a difference. I still get the error.

"Office_Novice" wrote:

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?

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

I don't get an error with your code... at least not #1004. I do get an error
#0 shown, but that is because you did not include an Exit Sub statement in
front of the ErrorHandler label, so your successful code is falling through
to the error handler below.

--
Rick (MVP - Excel)


"J. Caplan" wrote in message
...
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?


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

It worked ok in my brand new test worksheet. But I would add an "exit sub"
before the errorhandler: line.

Is your worksheet protected?

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?


--

Dave Peterson


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

I realized I was missing the End Sub after I submitted, but since that wasn't
the problem, I didn't bother re-posting.

I was about to reply that my worksheet is not protected, and I realized what
the problem is. I wrote this UDF but had not put in hooks to call it yet, so
I altered the code to a formula I wrote and had it call my function. I am
failing to write to the cells since I am in the middle of a formula when I am
trying to write these values.

If I call my function from a button click, it writes fine.

Thanks for the reply and sorry for jumping the gun and posting when it was
clearly "User Error"!!!

"Dave Peterson" wrote:

It worked ok in my brand new test worksheet. But I would add an "exit sub"
before the errorhandler: line.

Is your worksheet protected?

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?


--

Dave Peterson

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 to write data in array to range on worksheet eholz1 Excel Programming 1 January 30th 08 06:01 PM
Write from variant array into range Bharath Rajamani Excel Programming 1 May 9th 07 11:24 AM
Write 3d array to range [email protected] Excel Programming 2 December 31st 06 09:47 AM
write array to range muster Excel Programming 5 June 22nd 06 11:41 PM


All times are GMT +1. The time now is 08:41 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"