Solved

EXCEL VBA FUNCTION TRIGGERING

Posted on 2011-09-08
7
387 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
macro for same location path 2 19
macro for each dropdown 15 44
Copy value from a certain cell 5 24
Filling Blank Cells 14 8
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

947 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now