+ Reply to Thread
Results 1 to 7 of 7

How do I run a custom function only when a worksheet is selected?

  1. #1
    Registered User
    Join Date
    06-16-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    3

    Question How do I run a custom function only when a worksheet is selected?

    Hi all,

    Would appreciate some help on this.

    I'm not a VBA programmer, but found some snippet of code to make a custom function in a Module. What it does is it queries the web for a value. It works perfectly for me, problem is that now I have a lot of worksheets and many of the functions being called in numerous cells, so when Excel opens it hangs for some time.

    What I would like to do is only execute the function when a worksheet is selected. Is this possible? I think that would solve the problem with Excel being unresponsive.

    I tried surrounding the custom function with a private sub worksheet_activate() and then an end sub after end function, but it's giving me errors.

  2. #2
    Registered User
    Join Date
    06-11-2012
    Location
    Bangalore
    MS-Off Ver
    Excel 2007
    Posts
    86

    Re: How do I run a custom function only when a worksheet is selected?

    Dear Jack,

    In this workbook make the calculation option as manual.


    then paste this code in the sheet code area,for which upon activation u want to run these function.


    Please Login or Register  to view this content.
    Last edited by Leith Ross; 06-16-2012 at 08:09 PM. Reason: Added Code Tags

  3. #3
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: How do I run a custom function only when a worksheet is selected?

    Worksheet event code is stored on a worksheet module. To add it to your worksheet, do the following:

    Copy the code
    Select the worksheet in which you the code to run
    Right click on the sheet tab and choose View Code, to open the Visual Basic Editor.
    Where the cursor is flashing, choose Edit | Paste
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  4. #4
    Registered User
    Join Date
    06-16-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: How do I run a custom function only when a worksheet is selected?

    Thanks for the replies but is there a code that I can put in Module1? I have A LOT of worksheets. But I also tried that in a few sheets, and the function no longer worked if I do it that way. I also tried putting this:

    Please Login or Register  to view this content.
    ...after End Function, but Excel still hangs when I open it.

    Let me clarify what I want to do in case there is any misunderstanding.

    I have a custom function using Function MyCustomFuction() which is in Modules/Module1. I call this =MyCustomFunction("...") in numerous worksheets and cells. But when I open Excel, Excel seems to execute this function on all the worksheets and Excel freezes. I would like Excel to not execute this function automatically on all sheets and only execute it when I click on a worksheet tab.
    Last edited by Leith Ross; 06-16-2012 at 08:10 PM. Reason: Added Code Tags

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: How do I run a custom function only when a worksheet is selected?

    This might be what you mean

    Please Login or Register  to view this content.
    Workbook Event code should be added to the workbook code module:

    Copy the Excel VBA code that you want to use
    Select the workbook in which you want to store the code
    Hold the Alt key, and press the F11 key, to open the Visual Basic Editor
    In the Project Explorer, find your workbook, and open the list of Microsoft Excel Objects
    Right-click on the ThisWorkbook object, and choose View Code
    Where the cursor is flashing, choose Edit | Paste

  6. #6
    Registered User
    Join Date
    06-16-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: How do I run a custom function only when a worksheet is selected?

    Roy, thank you so much, your code and instructions works nicely now. Excel opens without freezing and the function only executes when I select a tab/sheet.

    Just one issue - if I save the entire xlsm file, Excel is freezing, I think it is executing the functions on sheets that weren't opened in a session. Is it possible to avoid that too?

  7. #7
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: How do I run a custom function only when a worksheet is selected?

    Really I would check your Functions - it's not good that they cause this to happen.

+ 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