0

Is is possible to use one UDF inside another?

For example Columns A,B,C,D.

UDF1(w,x) - Calculates the product of the maximum of A and the Maximum of B
UDF2(y,z) - Calculates the product of the maximum of C and maximum of D

UDF1 and UDF2 work fine.

UDF3 to calculate the product of UDF1 and UDF2 (as in cell calc is trivial)

Can I make UDF3 that take the as input (w,x,y,z) and then passes w,x to UDF1 and y,z to UDF2 - return the answers from UDF1 and UDF2 and calculate the product?

(I know there are other ways to do what I am describing - its the concept I am interested in) --

Background: I am trying to work up examples of programming inside Excel that starts by building simple UDFs and then recycling them (kinda like subroutines) -- again, I appreciate that there are better ways of doing this.

Cheers G

2
  • 1
    oops@pnuts: Just saw your comment :( Commented Nov 27, 2013 at 12:04
  • Um, why don't you just try it? Commented Nov 27, 2013 at 17:18

1 Answer 1

1

Can I make UDF3 that take the as input (w,x,y,z) and then passes w,x to UDF1 and y,z to UDF2 - return the answers from UDF1 and UDF2 and calculate the product?

Yes.

Here is an example. Paste this in a module. I am using SUM for demonstration.

Function MainF(rng1 As Range, rng2 As Range, _
rng3 As Range, rng4 As Range) As Long
    Dim x As Long, y As Long

    x = a(rng1, rng2)
    y = b(rng3, rng4)

    MainF = x + y
End Function

Function a(rng1 As Range, rng2 As Range) As Long
    a = rng1.Value + rng2.Value
End Function

Function b(rng1 As Range, rng2 As Range) As Long
    b = rng1.Value + rng2.Value
End Function

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

@pnuts: One should add [excel-vba] if working from Excel-VBA. [excel-vba-mac] is added when user wants to specify that he or she is working with mac else there is no need.
@pnuts - succinct and to the point. Works a treat - and aardvark123 - total mental block on where to start. Needed a nudge in the right direction. Thanks G

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.