Solved

EXCEL VBA FUNCTION TRIGGERING

Posted on 2011-09-08
7
425 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa‚Ķ

635 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