Results 1 to 10 of 10

Thread: [RESOLVED] Excel Range of Cells selection doesn't work

  1. #1

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Resolved [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

  2. #2
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,263

    Re: Excel Range of Cells selection doesn't work

    Cells of what?
    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

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  4. #4

  5. #5

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Excel Range of Cells selection doesn't work

    Quote Originally Posted by westconn1 View Post
    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?

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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

  7. #7

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    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.

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  9. #9

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    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?

  10. #10
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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
  •  



Click Here to Expand Forum to Full Width