Solved

EXCEL VBA FUNCTION TRIGGERING

Posted on 2011-09-08
7
377 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
Comment Utility
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
Comment Utility
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 80

Expert Comment

by:byundt
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:jamunsey
Comment Utility
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 80

Accepted Solution

by:
byundt earned 125 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Worked Perfectly.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

772 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

16 Experts available now in Live!

Get 1:1 Help Now