|
-
Nov 19th, 2012, 05:57 PM
#1
[RESOLVED] Excel Range of Cells selection doesn't work
This works
ActiveSheet.Range("H4:H10").Select
This gives application-defined or object-defined error. Why????
ActiveSheet.Range(Cells(4, 8), Cells(10, 8)).Select
Last edited by MartinLiss; Nov 19th, 2012 at 06:20 PM.
-
Nov 20th, 2012, 03:09 AM
#2
Re: Excel Range of Cells selection doesn't work
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Nov 20th, 2012, 03:25 AM
#3
Re: Excel Range of Cells selection doesn't work
ActiveSheet.Range(Cells(4, 8), Cells(10, 8)).Select
i would assume if you fully qualify the cells it should work
the above code leaves a possibility that the cells could be on a different sheet to the range object, even though the default for unqualified cells would be the default (active) worksheet
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Nov 20th, 2012, 08:29 AM
#4
Re: Excel Range of Cells selection doesn't work
 Originally Posted by Zvoni
Cells of what?
The cells of the active sheet of an Excel workbook.
-
Nov 20th, 2012, 08:31 AM
#5
Re: Excel Range of Cells selection doesn't work
 Originally Posted by westconn1
i would assume if you fully qualify the cells it should work
the above code leaves a possibility that the cells could be on a different sheet to the range object, even though the default for unqualified cells would be the default (active) worksheet
What do you mean when you say "fully qualify"? And why is the Range Cells index method different from the Range A1 method?
-
Nov 20th, 2012, 08:43 AM
#6
Re: Excel Range of Cells selection doesn't work
For Range you specified the parent object ( ActiveSheet.Range ), but for Cells you didn't.
If you don't specify a parent, Excel will make a guess... and there are some oddities to that which can cause errors or bugs.
Try this:
Code:
ActiveSheet.Range(ActiveSheet.Cells(4, 8), ActiveSheet.Cells(10, 8)).Select
By the way, it is best to also avoid selecting things if you can, as it is slow, and the current selection can change at any moment outside of your control (particularly if the user or other code is working with Excel).
Generally you can easily avoid it by merging two lines of code (removing .Select and Selection), so if your code is like this:
Code:
ActiveSheet.Range("H4:H10").Select
Selection.Paste
...the quicker/safer equivalent is this:
Code:
ActiveSheet.Range("H4:H10").Paste
-
Nov 20th, 2012, 08:54 AM
#7
Re: Excel Range of Cells selection doesn't work
ActiveSheet.Range(ActiveSheet.Cells(4, 8), ActiveSheet.Cells(10, 8)).Select
Thanks Si, I'm sure that's it. In my code I bounce around between sheets and I ran into other places where after selecting a given sheet I had to use ActiveSheet even though I had explicitly selected the sheet. I just never thought of using ActiveSheet within the range. I'll keep the other information in mind too.
-
Nov 20th, 2012, 03:24 PM
#8
Re: Excel Range of Cells selection doesn't work
ActiveSheet even though I had explicitly selected the sheet.
you should avoid using activesheet, better to use fully qualified ranges book-sheet-range, or better, set worksheet objects and work with those through the code
What do you mean when you say "fully qualify"?
sorry i did not explain more fully
the default sheet is normally the active sheet, but in cases it has just been selected or activated, it may not be until the code finishes or a doevents is processed
I just never thought of using ActiveSheet within the range.
easy in a with block
Code:
with somesheet
.range(.cells(2, 1), .cells(4,2)).select
end with
but best to avoid selecting
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Dec 9th, 2012, 10:28 AM
#9
Re: [RESOLVED] Excel Range of Cells selection doesn't work
Si (or westconn1), I'm getting back to this and I find that I'm still confused. Can you show me a complete set of code that specifies both the source and destination sheets?
-
Dec 9th, 2012, 03:34 PM
#10
Re: [RESOLVED] Excel Range of Cells selection doesn't work
the best way is to totally avoid selecting or activating sheets or ranges, just use full qualified ranges
fully qualified should be from workbook level down like
workbooks("bookx.xls").sheets("mysheet").range("a3").copy
when working with multiple sheets, either within the same book or not, best to assign a worksheet objects
Code:
set somesht = workbooks("bookx.xls").sheets("mysheet")
with somesht
.range(.cells(2, 2)), .cells(2, 4)).copy
' you can see that outside a with block, you should have to specify the book /sheet (or sheet object) for each range of cells, to be on the same sheet as the parent range
end with
secondsht.cells(4, 6).pastespecial
' where secondsht was previously assigned and target range is a single cell (for paste)
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
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
|