Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Using a Calculated Value from a Custom Function in the Calling Sub
I do not know how to use a calculated value from a custom function in
its calling subprocedure. In other words, how do you move the calculated value back into the sub that called it? Here is a simple example of a function (AddValues) that adds two values, which are arguments passed from its calling procedure (TotalValues). If you run the sub with the Call AddValues(Value1, Value2) statement, the function returns a Total = 12, but how can that value be transferred back to the sub and used there, say in additional calculations(for which no code is currently shown)? As an alternative to the Call AddValues(Value1, Value2) statement, I tried to import the calculated value of 12 directly into the sub by assigning a variable mytotal in the sub to the Application.Run("MacroPractice.xls!AddValues", 5, 7) statement. The result was that the two arguments of 5 and 7 were passed from the sub to the function and 12 was still calculated for Total, but nothing was returned to the mytotal variable in the sub. Function AddValues(Val1 As Integer, Val2 As Integer) as Integer Dim Total As Integer Total = Val1 + Val2 MsgBox Total End Function Public Sub TotalValues() Dim Value1 As Integer, Value2 As Integer, mytotal As Integer Value1 = 5 Value2 = 7 Call AddValues(Value1, Value2) mytotal = Application.Run("MacroPractice.xls!AddValues", 5, 7) MsgBox mytotal End Sub I use Excel for Mac 2004, which is similar to Excel for Windows 2003. Thank you. |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Pivot table, IF function, calculated item versus calculated field | Excel Discussion (Misc queries) | |||
Custom Calculated Items in a PivotTable | Excel Worksheet Functions | |||
How do you get calling cell for custom function | Excel Programming | |||
Custom Function not being auto calculated when cells change..help? | Excel Worksheet Functions | |||
Calling a Custom Function within a Procedure | Excel Programming |