+ Reply to Thread
Results 1 to 15 of 15

How do you get this event macro to work?

  1. #1
    Registered User
    Join Date
    03-01-2013
    Location
    Pittsburgh, Pennsylvania
    MS-Off Ver
    Excel 2010
    Posts
    6

    How do you get this event macro to work?

    This is a macro I found online to freeze the first tab in my workbook. I am new to VBA, so please explain in details.

    Please Login or Register  to view this content.
    Last edited by arlu1201; 03-02-2013 at 07:10 AM.

  2. #2
    Valued Forum Contributor Hawkeye16's Avatar
    Join Date
    02-27-2013
    Location
    Holland
    MS-Off Ver
    ├•┤ Pew Pew
    Posts
    441

    Re: How do you get this macro to work?

    Tome it does not look like it freezes anything. It just moves tabs around. Also you pass in sh but never use it. This looks like a poor coding attempt to me. Not even sure what it is attempting (what you mean by freeze).

  3. #3
    Registered User
    Join Date
    03-01-2013
    Location
    Pittsburgh, Pennsylvania
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: How do you get this macro to work?

    Okay. The problem is I have about 30 different worksheet tabs. I want my very first tab in the workbook to not move or always stay visible as you scroll through all the tabs. Is there a macro that would accomplish this? Thanks for the help!

  4. #4
    Registered User
    Join Date
    01-19-2009
    Location
    UK
    MS-Off Ver
    2007
    Posts
    60

    Re: How do you get this macro to work?

    Hawkeye, it moves the selected Worksheet so it's the second tab along the bottom. Speaking of poor attempts, did you even try to read or test the code before you bashed it?

    kdanilko, to make the code work you need to open the Visual Basic Editor, double click "ThisWorkbook" and put the code in there. You can then close VBE and when you select a worksheet you will notice the tab changes position.

    That code is a bit messy though, you should use the following code instead:

    Please Login or Register  to view this content.
    Let me know if it works for you.
    .
    - AKK9 -

  5. #5
    Valued Forum Contributor Hawkeye16's Avatar
    Join Date
    02-27-2013
    Location
    Holland
    MS-Off Ver
    ├•┤ Pew Pew
    Posts
    441

    Re: How do you get this macro to work?

    It takes sh as an input and never uses it. I stand by my poor coding comment. I pasted it and tried to replicate exactly what it was doing and it crashed my excel, and somehow anytime I opened that workbook it crashed excel immediately. Wasn't on a workbook open or anything so kind of weird - but yea. Bad code.

  6. #6
    Registered User
    Join Date
    01-19-2009
    Location
    UK
    MS-Off Ver
    2007
    Posts
    60

    Re: How do you get this macro to work?

    No, it's an event that occurs when a worksheet is activated. Sh is required, whether you use it or not is up to you.

    Anyway, my point wasn't that the code was great (notice I replaced it with 1 line that does the same job). It's that it's pointless telling someone "bad code" after they've told you they're new to VBA, and they're posting to the forum specifically asking for help.

  7. #7
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: How do you get this event macro to work?

    kdanilko,

    Welcome to the forum.

    I have added code tags to your post. As per forum rule 3, you need to use them whenever you put any code in your post. Please add them in future. If you need more information on how to use them, check my signature below this post.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: How do you get this event macro to work?

    There seems to be some confusion on what role Sh plays here. Let me explain what this object variable is.

    This is a Workbook level event. This event tracks the Worksheet level event SheetActivate. The object variable Sh (Sheet) is passed from the Worksheet event to the Workbook level event after the sheet is activated. Sh always points to the sheet that became the ActiveSheet in the workbook. Because it is a Worksheet object, you have access to all properties and methods of the original sheet.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  9. #9
    Valued Forum Contributor Hawkeye16's Avatar
    Join Date
    02-27-2013
    Location
    Holland
    MS-Off Ver
    ├•┤ Pew Pew
    Posts
    441

    Re: How do you get this event macro to work?

    So leith, is it required (I am no expert but I have never seen a required input that is not even used like that). If I understand you could use sh.activate or similar statements. But is it required for an event like this? I guess I am less knowledgeable about the SheetActivate method than I thought.

  10. #10
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: How do you get this event macro to work?

    Hello Hawkeye16,

    The variable Sh is automatically provided to you by the event procedure. The user does not declare this variable nor is the user required to use it.

  11. #11
    Registered User
    Join Date
    01-19-2009
    Location
    UK
    MS-Off Ver
    2007
    Posts
    60

    Re: How do you get this event macro to work?

    Thanks.

    Maybe I explained it weird, to clarify, what I meant by 'required' is that the Sh parameter is required. As in, the event cannot fire without the Sh parameter being passed because it's required which is why I said "whether you use it or not is up to you".

  12. #12
    Registered User
    Join Date
    03-01-2013
    Location
    Pittsburgh, Pennsylvania
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: How do you get this event macro to work?

    Hello Everyone,

    Thank you for all of your help.
    AKK9, after pasting your code into VBE, I was unable to run the macro because it did not appear anywhere in the macro list. Again, I may be doing something wrong because I have no previous experience with VBE. Any suggestions?

    Thanks again

  13. #13
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: How do you get this event macro to work?

    you do not run it-it runs automatically when you switch between sheets.
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  14. #14
    Registered User
    Join Date
    01-19-2009
    Location
    UK
    MS-Off Ver
    2007
    Posts
    60

    Re: How do you get this event macro to work?

    kdanilko, once you have pasted your code it will run every time you select a sheet. Make some new sheets, and look at how the tabs reorganise whenever you select one.

    Whenever you select a sheet, this code will move it so it's 2nd along the bottom. So you can access your first sheet easily, regardless of which tab you have selected.

    Does it help?

  15. #15
    Registered User
    Join Date
    03-01-2013
    Location
    Pittsburgh, Pennsylvania
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: How do you get this event macro to work?

    Thanks AKK9!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1