Excel User Group
Microsoft Excel blogs, forums, files. Read, ask questions, provide answers.

Trying to understand Object Heirarchy

Latest post Tue, May 27 2008 6:46 PM by Zack Barresse. 4 replies.
  • Thu, May 22 2008 3:55 PM

    • ajandco
    • Top 150 Contributor
    • Joined on Thu, May 22 2008
    • Posts 2
    • Points 58

    Trying to understand Object Heirarchy

    I am trying to get to grips with object heirarchy

    Could someone please let me know why the following work/ don't work (as noted in the comments) before I loose my marbles completely - any insight would be appreciated:

    Many thanks - Alastair

    .
    .

    Set AppExcel = CreateObject("Excel.Application")

    set wBook = AppExcel.Workbooks.Open("Temp.xls")

    Set mysheet = wBook.Sheets(1)

    .
    .

    wBook.WorkSheets(1).Select
    wBook.WorkSheets(1).Activate

    'These lines performs without error

    AppExcel.Range("A2").Select
    AppExcel.Range(AppExcel.Selection, AppExcel.Selection.End(xlDown)).Select
    AppExcel.Selection.ClearContents

    'I thought the following would be equivalent to the above lines - the middle line Fails on error 'Object variable or With block variable not set

    With mysheet
                       
       .Range("A2").Select
       .Range(Selection, Selection.End(xlDown)).Select     'Fails on error 'Object variable or With block variable not set 
       .Cells.ClearContents
               
    End With

    Filed under:
    • Post Points: 37
  • Thu, May 22 2008 5:19 PM In reply to

    Re: Trying to understand Object Heirarchy

    If you look at Selection in VBA help you will see that it is part of the Application or Window objects. It is not qualified in your second example, whereas it is within the Application object in the first.

    Regards

    Bob

    • Post Points: 5
  • Fri, May 23 2008 8:37 AM In reply to

    • Rob Bovey
    • Top 75 Contributor
    • Joined on Sat, Apr 19 2008
    • USA
    • Posts 4
    • Points 136
    • MVP

    Re: Trying to understand Object Heirarchy

    To further Bob's answer, it is rarely necessary or desirable to explicitly select objects in Excel prior to operating on them. I would suggest rewriting your code to the following equivalent:

    mysheet.Range(mysheet.Range("A2"), mysheet.Range("A2").End(xlDown)).ClearContents

    Rob Bovey

    www.appspro.com

    • Post Points: 5
  • Fri, May 23 2008 12:38 PM In reply to

    • ajandco
    • Top 150 Contributor
    • Joined on Thu, May 22 2008
    • Posts 2
    • Points 58

    Re: Trying to understand Object Heirarchy

     Thanks to you both for your help.  The suggestion works perfectly and the methodology is beginning to gel (I hope!). 

    Filed under:
    • Post Points: 21
  • Tue, May 27 2008 6:46 PM In reply to

    • Zack Barresse
    • Top 75 Contributor
    • Joined on Sat, Apr 19 2008
    • Oregon, USA
    • Posts 4
    • Points 136
    • MVP

    Re: Trying to understand Object Heirarchy

    The other thing to think about here is if you have blanks in your data or any type of non-contiguous data structure.  If this is a possibility for you, the xldown may not be the right option for you, but rather go from the bottom up to find the last cell and keep your dynamic range.  If this is the case, you could use a simple change...

     

    mysheet.Range(mysheet.Range("A2"), mysheet.Range("A" & mysheet.Rows.Count).End(xlUp)).ClearContents

     

    • Post Points: 5
Page 1 of 1 (5 items) | RSS
Copyright Excel User Group and the relevant contributors, 2008. All Trade Marks acknowledged. This site is a peer-to-peer site and NOT affiliated in any way to Microsoft Corp. All rights reserved.