Help with vba function

TheAaron

New Member
Joined
Oct 1, 2015
Messages
11
Hello, I have the following function:

Function Fun(z)
Fun = z - x - (((((z - Dato1) * Dato1P) + Dato3) - (((((z - Dato1) * Dato1P) * Dato2) + Dato4) * R)) - Dato5)
End Function

There are several varibles within it, the thing is that it just does not work, for example:
Fun(20) returns 20

What can I do?
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Fun(Z) = 20 because only "z" has a value in your formula... 20 - nothing = 20 If your variables(x, Dato1, Dato2, etc.) have any values(?) they need to be scoped/declared so as to allow your function to use them. Public Dim then in module code if necessary. HTH. Dave
ps. Perhaps your function should also be module code and declared as...
Code:
Public Function Fun(z as Double) as Double
... great name for a FUNction!
 
Last edited:
Upvote 0
What can I do?

Perhaps start by telling us what your function is meant to do, and what are x, Dato1, Dato1P, Dato2 etc?

Unless you have declared your variables at module level and given them a value elsewhere, they will all have value zero inside your function. So your function simplifies to:

Fun = z - 0 - (((((z - 0) * 0) + 0) - (((((z - 0) * 0) * 0) + 0) * 0)) - 0)
= z

So Fun(20)= 20

Presumably you mean to pass the variables as arguments for the function, in the same way you have with z?

I also recommend using Option Explicit in each of your code modules. Then the compiler will give you the more helpful message "Variable not defined".
 
Upvote 0
Code:
Function Fun(byval z as integer, x as integer, Dato1 as integer,Dato2 as integer, Dato3 as integer, Dato4 as integer, Dato1P as integer, r as integer) as integer
Fun = z - x - (((((z - Dato1) * Dato1P) + Dato3) - (((((z - Dato1) * Dato1P) * Dato2) + Dato4) * R)) - Dato4)
End Function
try this
fun(1,2,3,4,5,6,7,8)=1 - 2 - (((((1 - 3) * 7) + 5) - (((((1 - 3) * 7) * 4) + 6) * 8)) - 6)=-386
 
Last edited:
Upvote 0
Thanks to all!
My variables are defined later on my module, I'll see what I cand do with you tips.
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,323
Members
449,077
Latest member
jmsotelo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top