|
-
Apr 22nd, 2005, 09:03 AM
#1
Thread Starter
Member
Setting Excel Range.Value when sheet not selected?
I'm having a problem with XL2000 when setting the values of a Range object on a given worksheet to the contents of an array when that worksheet is not active (a different worksheet is selected/active). It works perfectly with this method:
Code:
Dim myArray(1 to 2, 1 to 5) as Double
'(Set myArray values here)
Worksheet("1").Range("A1:E2").Value = myArray
but I need to be able to use the "cells" method of defining a Range object. The following method does not work when the sheet containing the Range being set is not the active worksheet:
Code:
Dim myArray(1 to 2, 1 to 5) as Double
'(Set myArray values here)
Worksheet("1").Range(Cells(1 ,1), Cells(2,5)).Value = myArray
This method raised error #1004: "Application-defined or Object-defined error." I would greatly prefer to use the second method in my code because it suits my program much better. Does anyone know how to make it work without selecting the sheet?
-
Apr 22nd, 2005, 10:19 AM
#2
Addicted Member
Re: Setting Excel Range.Value when sheet not selected?
Humm is the code in a module or on the sheet object?
Try using Sheets("Sheet1") instead of worksheet.
Last edited by D-niss; Apr 22nd, 2005 at 10:46 AM.
-
Apr 22nd, 2005, 10:44 AM
#3
Thread Starter
Member
Re: Setting Excel Range.Value when sheet not selected?
I just tried that idea. It raises the same error. I hadn't tried that previously, only because I know that VBA uses the activeworkbook when the workbook is excluded from the statement. Thanks for the try.
-
Apr 22nd, 2005, 10:47 AM
#4
Addicted Member
Re: Setting Excel Range.Value when sheet not selected?
 Originally Posted by Johnboy
I just tried that idea. It raises the same error. I hadn't tried that previously, only because I know that VBA uses the activeworkbook when the workbook is excluded from the statement. Thanks for the try. 
Yes I know, my bad. I misread your post. Is the code in a module?
-
Apr 22nd, 2005, 11:25 AM
#5
Thread Starter
Member
Re: Setting Excel Range.Value when sheet not selected?
 Originally Posted by D-niss
Yes I know, my bad. I misread your post. Is the code in a module?
Yes, it's in a module. I have a really cool method for saving data from my application into a binary file. It runs faster when I then reload the data using the method of setting a range = an array, rather than looping through individual cells. This procedure refers to around 20 separate worksheets that are all identical. So I place it in a module to call it globally.
-
Apr 23rd, 2005, 01:20 AM
#6
Addicted Member
Re: Setting Excel Range.Value when sheet not selected?
 Originally Posted by Johnboy
Yes, it's in a module. I have a really cool method for saving data from my application into a binary file. It runs faster when I then reload the data using the method of setting a range = an array, rather than looping through individual cells. This procedure refers to around 20 separate worksheets that are all identical. So I place it in a module to call it globally.
It doesn't work for me either. I guess the sheet has to be activate.
You can use Worksheets(1).activate before your routine.
I've never ran into that prblm. I must be doing it some other way.
Perhaps if you show how you're doing this, I could give you some
suggestions.
Regards,
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|