Solved

EXCEL VBA FUNCTION TRIGGERING

Posted on 2011-09-08
7
417 Views
Last Modified: 2012-05-12
How do you cause a custom function to execute when changes are made to worksheets / cells?

So, if I have a custom function in a cell, that uses data from multiple worksheets, I need for the custom function to run based on those changes.  Obviously, it may or may not impact the value returned into the cell by the function, but I need to make sure it runs.  In other words, I want it to behave like any other excel function.

My custom function is currently working, but If I change values in cells within the worksheet, it does not re-execute.

Any help on this would be greatly appreciated.
0
Comment
Question by:jamunsey
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 5

Expert Comment

by:slycoder
ID: 36507104
I like to put this in "ThisWorkbook"

Sub auto_open()

   ' Run the macro any time a entry is made

    For Each myWorksheet In ThisWorkbook.Worksheets
        myWorksheet.OnEntry = "MyFunction"
    Next
   
End Sub


Public Sub MyFunction()
    MsgBox ("changes made")
End Sub


Be careful with recursive calls

0
 
LVL 5

Expert Comment

by:slycoder
ID: 36507111
By the way - You should make it a habit to close and re-open the workbook whenever you create/modify a "Sub auto_open"

This is VERY important - otherwise, it won't function as desired, or not at all.

Thanks
0
 
LVL 81

Expert Comment

by:byundt
ID: 36507157
While I personally prefer to pass the arguments and therefore force recalculation only when required, you might consider making your function volatile as discussed in http://www.decisionmodels.com/calcsecretsj.htm
Volatile functions (even UDFs) recalculate whenever anything changes in any open workbook.

Function MyFunc()
Dim MyVar As Variant
Application.Volatile(True)
'Do Stuff
MyFunc = SomeCalculation
End Function
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

Author Comment

by:jamunsey
ID: 36507412
Thanks for the input.  I am thinking the Application.Volatile(TRUE) may work, but I want to clarify that for the worksheet that contains the function, it exists in many cells.  It may be somewhere in the neighborhood of a hundred cells.  Each function call has different inputs.  For example:

                       C1 = 102345
A2:  rtuo         C2  = Analyze_data($C$1, A2)
A3:  wejo        C3  = Analyze_data($C$1, A3)
A4:  bmnr       C4  = Analyze_data($C$1, A4)

So, the Function Analyze_data will run on the 2 pieces of data that are passed to it, but it also calls several support functions that access other worksheet cells to complete the analysis and return the final result in the given cell (like Cell C2).  So, I could change data on one of the other worksheets, and want to make sure that Analyze_data creates the updated result.  

With this additional explanation, Do you still think the Application.Volatile(TRUE) will work?  

Thanks so much.
0
 
LVL 81

Accepted Solution

by:
byundt earned 125 total points
ID: 36507553
Application.Volatile will make your function update even if you change a value in a completely different workbook. I believe it will handle your situation exactly as you describe.

You must evaluate whether Application.Volatile makes your workbook recalc time too sluggish. Remember that it recalculates every time anything changes in any open workbook. If you manipulate a lot of data, then there could be an issue.

Application.Volatile will not update in response to format changes. I don't believe you are wanting that feature, but some people do--and they are disappointed by this gap between feature and requirement.
0
 

Author Comment

by:jamunsey
ID: 36507651
I performed some preliminary testing and the Application.Volatile worked like a champ.  I will do some additional testing, but I believe I will be good to go with this as the approach.  'slycode' ... thanks for your input as well ... much appreciated.
0
 

Author Closing Comment

by:jamunsey
ID: 36507653
Worked Perfectly.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question