Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

EXCEL VBA FUNCTION TRIGGERING

Posted on 2011-09-08
7
Medium Priority
?
431 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 500 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
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…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

715 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