Consulting

Results 1 to 10 of 10

Thread: Solved: Procedure "Sheet_Activate_1()" Does What?

  1. #1

    Solved: Procedure "Sheet_Activate_1()" Does What?

    I was looking through the contents of the VBAX Code Library (neat stuff!), and I came across two procedures which I didn't understand the purpose of. The first is:
    [VBA]Sub Sheet_Activate_1()
    Sheets(1).Activate
    [A1].Activate
    End Sub[/VBA]
    What does this Sub do for you? It appears to activate (select?) the first worksheet in the workbook, then (if I understand the shorthand) select cell "A1". Why would one need a procedure to do that?? Is Activate in this context equivalent to Select?

  2. #2
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    This macro will activate the first sheet in the workbook then activate A1. In this case Activate is the same as Select.

    I am not sure what the point of the macro is. Perhaps it is used at the end of a macro to select the first sheet and A1 so that will be what is selected when the macro is completed.

  3. #3
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Alternately, if you were to use it in a workbook open (and/or) other event, you could ensure that you were always returning the user to a specified place. I submitted something similar to the KB (by request) for exactly that purpose.

    The only reason that I would write a separate procedure to do this would be to call it from multiple places, in the following format:
    [vba]Call Sheet_Activate_1[/vba]

    The Call command isn't exactly required, but I prefer to put it in for self documenting purposes.

    It's pretty much a "utility code" type of procedure, IMHO.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  4. #4
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Quote Originally Posted by kpuls
    .. The Call command isn't exactly required ..
    I think the word Ken was searching for was it's "implied". As are things like Activesheet and ActiveWorkbook, etc.

    Cyberdude, there is a difference between Activate and Select. The difference being you can only Activate one Object at a time. You cannot Activate two Ranges at one time. You also cannot Select any hidden sheet or Select more than one Range at a time, the terms coined for that is Union and Intersect, both having their own traits and purposes. Colo has a nice write up on his site about Activating and Selecting sheets and the differences. The link is here: http://puremis.net/excel/code/066.shtml

    HTH

  5. #5
    VBAX Newbie
    Joined
    Apr 2005
    Posts
    4
    Location
    I recently came upon that piece of code as well, it helps me get back to certain sheets of which the user changed the name. Sheets ("sheets1") won't work if the user changed the name to "data", for instance. While this code would.

    Ofcourse i'm not sure how it would react if users changed the *location* of sheets, but that doesn't happen nearly as much as renaming.

  6. #6
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by Maurits
    I recently came upon that piece of code as well, it helps me get back to certain sheets of which the user changed the name. Sheets ("sheets1") won't work if the user changed the name to "data", for instance. While this code would.

    Ofcourse i'm not sure how it would react if users changed the *location* of sheets, but that doesn't happen nearly as much as renaming.

    Hi Maurits,

    It works like this [VBA]Sub Firstsheetactivate()
    'activates 1st sheet positioned on the left in w/book
    '(ignores given and code-names)
    Sheets(1).Activate
    End Sub[/VBA]

    [VBA]Sub sheet1activate1()
    'activates sheet with given name "Sheet1"
    '(ignores position & code-name, fails if sheet is re-named)
    Sheets("Sheet1").Activate
    End Sub[/VBA]

    [VBA]

    Sub sheet1activate2()
    'activates sheet with the code-name sheet1
    'ignores position and any given name (re-naming)
    Sheet1.Activate
    End Sub

    [/VBA]

    Regards,
    John
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  7. #7
    VBAX Newbie
    Joined
    Apr 2005
    Posts
    4
    Location
    How incredibly cool. Awesome john, thanks!

  8. #8
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hey John,

    It occurs to me that you should write an article to explain that. It's a very useful thing to know, and could be very helpful to many new VBA'ers. Well explained!

    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  9. #9
    I agree! Very enlightening. Thanks again to you all.

  10. #10
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by kpuls
    Hey John,

    It occurs to me that you should write an article to explain that. It's a very useful thing to know, and could be very helpful to many new VBA'ers. Well explained!


    Sigh.... No rest for the wicked is there. Will think about it...
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •