ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Worksheet Functions (https://www.excelbanter.com/excel-worksheet-functions/)
-   -   Custom functions (https://www.excelbanter.com/excel-worksheet-functions/195020-custom-functions.html)

Palpha32

Custom functions
 
One of my custom functions calculates certain values("a" and "b") and then
calculates a result relying on "a" and "b". Am I able to use "a" and "b" in
another function by getting their value from the first function?

function one(x,y,z)
a = x + y
b = x + z
c = a * b
end function

function two(x,y,z)
a = x + y
b = x + z
d = a / b
end function

Thanks
Peter

Rick Rothstein \(MVP - VB\)[_933_]

Custom functions
 
If you declare your variables in the (General)(Declarations) section of the
code module where your function are (that is, declare them outside of any
procedures), then they will be "global" to the code module and **any**
procedure within that code module will be able to read AND CHANGE them. That
"and change them" is very important, especially if you do not declare all
your variables since then you could accidentally use the same variable name
in an unrelated procedure, change the value there without realizing the
effect doing so will have elsewhere and thus screw up some other procedure
that is dependent on those variables. If you give your global variables some
distinctive feature (say, start each name with "global"), then the odds of
accidentally tripping over them is greatly reduced. Now, with all that said,
here is an example...

Dim A As Long
Dim B As Long

Function One(X, Y, Z)
A = X + Y
B = X + Z
One = A * B
End Function

Function Two()
Two = A / B
End Function

Rick


"Palpha32" wrote in message
...
One of my custom functions calculates certain values("a" and "b") and
then
calculates a result relying on "a" and "b". Am I able to use "a" and "b"
in
another function by getting their value from the first function?

function one(x,y,z)
a = x + y
b = x + z
c = a * b
end function

function two(x,y,z)
a = x + y
b = x + z
d = a / b
end function

Thanks
Peter



Palpha32

Custom functions
 
Thanks Rick, that works well.....except where I use function one in another
function eg.

function three(x)
three = one / x
end function

is this possible?

Peter

"Rick Rothstein (MVP - VB)" wrote:

If you declare your variables in the (General)(Declarations) section of the
code module where your function are (that is, declare them outside of any
procedures), then they will be "global" to the code module and **any**
procedure within that code module will be able to read AND CHANGE them. That
"and change them" is very important, especially if you do not declare all
your variables since then you could accidentally use the same variable name
in an unrelated procedure, change the value there without realizing the
effect doing so will have elsewhere and thus screw up some other procedure
that is dependent on those variables. If you give your global variables some
distinctive feature (say, start each name with "global"), then the odds of
accidentally tripping over them is greatly reduced. Now, with all that said,
here is an example...

Dim A As Long
Dim B As Long

Function One(X, Y, Z)
A = X + Y
B = X + Z
One = A * B
End Function

Function Two()
Two = A / B
End Function

Rick


"Palpha32" wrote in message
...
One of my custom functions calculates certain values("a" and "b") and
then
calculates a result relying on "a" and "b". Am I able to use "a" and "b"
in
another function by getting their value from the first function?

function one(x,y,z)
a = x + y
b = x + z
c = a * b
end function

function two(x,y,z)
a = x + y
b = x + z
d = a / b
end function

Thanks
Peter




Rick Rothstein \(MVP - VB\)[_953_]

Custom functions
 
Yes, you can embed one function call in another (as long as the return type
is compatible with what the parent function is expecting). In your example,
you have this line...

Three = One / X

but one is declared like this...

Function One(X, Y, Z)

so you have to specify the arguments when you call it. Your assignment line
will have to look like this..

Three = One(Var1, Var2, Var3) / X

where Var1, Var2 and Var3 have scope within your Three function. I thought
the idea behind your asking about using the Global variables was so that you
wouldn't have to do this?

Rick


"Palpha32" wrote in message
...
Thanks Rick, that works well.....except where I use function one in
another
function eg.

function three(x)
three = one / x
end function

is this possible?

Peter

"Rick Rothstein (MVP - VB)" wrote:

If you declare your variables in the (General)(Declarations) section of
the
code module where your function are (that is, declare them outside of any
procedures), then they will be "global" to the code module and **any**
procedure within that code module will be able to read AND CHANGE them.
That
"and change them" is very important, especially if you do not declare all
your variables since then you could accidentally use the same variable
name
in an unrelated procedure, change the value there without realizing the
effect doing so will have elsewhere and thus screw up some other
procedure
that is dependent on those variables. If you give your global variables
some
distinctive feature (say, start each name with "global"), then the odds
of
accidentally tripping over them is greatly reduced. Now, with all that
said,
here is an example...

Dim A As Long
Dim B As Long

Function One(X, Y, Z)
A = X + Y
B = X + Z
One = A * B
End Function

Function Two()
Two = A / B
End Function

Rick


"Palpha32" wrote in message
...
One of my custom functions calculates certain values("a" and "b") and
then
calculates a result relying on "a" and "b". Am I able to use "a" and
"b"
in
another function by getting their value from the first function?

function one(x,y,z)
a = x + y
b = x + z
c = a * b
end function

function two(x,y,z)
a = x + y
b = x + z
d = a / b
end function

Thanks
Peter





Palpha32

Custom functions
 
Rick, I think I am going around in circles. I have five functions which all
are independent. All of them rely on a calculation "valid" so that if
valid=false then "0" and the function ends. It seems to me that making the
same calculation (with the same variables) seems a waste, hence the first
question. There are five calculations similar to "valid" that are used in
two or more functions. There is also another case where the result of one
function is used as a variable other functions, hence the second question.

Thanks
Peter

"Rick Rothstein (MVP - VB)" wrote:

Yes, you can embed one function call in another (as long as the return type
is compatible with what the parent function is expecting). In your example,
you have this line...

Three = One / X

but one is declared like this...

Function One(X, Y, Z)

so you have to specify the arguments when you call it. Your assignment line
will have to look like this..

Three = One(Var1, Var2, Var3) / X

where Var1, Var2 and Var3 have scope within your Three function. I thought
the idea behind your asking about using the Global variables was so that you
wouldn't have to do this?

Rick


"Palpha32" wrote in message
...
Thanks Rick, that works well.....except where I use function one in
another
function eg.

function three(x)
three = one / x
end function

is this possible?

Peter

"Rick Rothstein (MVP - VB)" wrote:

If you declare your variables in the (General)(Declarations) section of
the
code module where your function are (that is, declare them outside of any
procedures), then they will be "global" to the code module and **any**
procedure within that code module will be able to read AND CHANGE them.
That
"and change them" is very important, especially if you do not declare all
your variables since then you could accidentally use the same variable
name
in an unrelated procedure, change the value there without realizing the
effect doing so will have elsewhere and thus screw up some other
procedure
that is dependent on those variables. If you give your global variables
some
distinctive feature (say, start each name with "global"), then the odds
of
accidentally tripping over them is greatly reduced. Now, with all that
said,
here is an example...

Dim A As Long
Dim B As Long

Function One(X, Y, Z)
A = X + Y
B = X + Z
One = A * B
End Function

Function Two()
Two = A / B
End Function

Rick


"Palpha32" wrote in message
...
One of my custom functions calculates certain values("a" and "b") and
then
calculates a result relying on "a" and "b". Am I able to use "a" and
"b"
in
another function by getting their value from the first function?

function one(x,y,z)
a = x + y
b = x + z
c = a * b
end function

function two(x,y,z)
a = x + y
b = x + z
d = a / b
end function

Thanks
Peter





Rick Rothstein \(MVP - VB\)[_954_]

Custom functions
 
I can't tell from this last post of yours... do you still have a pending
question or do you know how to continue now?

Rick


"Palpha32" wrote in message
...
Rick, I think I am going around in circles. I have five functions which
all
are independent. All of them rely on a calculation "valid" so that if
valid=false then "0" and the function ends. It seems to me that making
the
same calculation (with the same variables) seems a waste, hence the first
question. There are five calculations similar to "valid" that are used in
two or more functions. There is also another case where the result of one
function is used as a variable other functions, hence the second question.

Thanks
Peter

"Rick Rothstein (MVP - VB)" wrote:

Yes, you can embed one function call in another (as long as the return
type
is compatible with what the parent function is expecting). In your
example,
you have this line...

Three = One / X

but one is declared like this...

Function One(X, Y, Z)

so you have to specify the arguments when you call it. Your assignment
line
will have to look like this..

Three = One(Var1, Var2, Var3) / X

where Var1, Var2 and Var3 have scope within your Three function. I
thought
the idea behind your asking about using the Global variables was so that
you
wouldn't have to do this?

Rick


"Palpha32" wrote in message
...
Thanks Rick, that works well.....except where I use function one in
another
function eg.

function three(x)
three = one / x
end function

is this possible?

Peter

"Rick Rothstein (MVP - VB)" wrote:

If you declare your variables in the (General)(Declarations) section
of
the
code module where your function are (that is, declare them outside of
any
procedures), then they will be "global" to the code module and **any**
procedure within that code module will be able to read AND CHANGE
them.
That
"and change them" is very important, especially if you do not declare
all
your variables since then you could accidentally use the same variable
name
in an unrelated procedure, change the value there without realizing
the
effect doing so will have elsewhere and thus screw up some other
procedure
that is dependent on those variables. If you give your global
variables
some
distinctive feature (say, start each name with "global"), then the
odds
of
accidentally tripping over them is greatly reduced. Now, with all that
said,
here is an example...

Dim A As Long
Dim B As Long

Function One(X, Y, Z)
A = X + Y
B = X + Z
One = A * B
End Function

Function Two()
Two = A / B
End Function

Rick


"Palpha32" wrote in message
...
One of my custom functions calculates certain values("a" and "b")
and
then
calculates a result relying on "a" and "b". Am I able to use "a"
and
"b"
in
another function by getting their value from the first function?

function one(x,y,z)
a = x + y
b = x + z
c = a * b
end function

function two(x,y,z)
a = x + y
b = x + z
d = a / b
end function

Thanks
Peter






Palpha32

Custom functions
 
Thanks Rick, it is working now after a weekend of R&R. I don't understand the
bit about embedding functions and what I tried did not work so I will leave
that bit alone. Thanks again'
Peter

"Rick Rothstein (MVP - VB)" wrote:

I can't tell from this last post of yours... do you still have a pending
question or do you know how to continue now?

Rick


"Palpha32" wrote in message
...
Rick, I think I am going around in circles. I have five functions which
all
are independent. All of them rely on a calculation "valid" so that if
valid=false then "0" and the function ends. It seems to me that making
the
same calculation (with the same variables) seems a waste, hence the first
question. There are five calculations similar to "valid" that are used in
two or more functions. There is also another case where the result of one
function is used as a variable other functions, hence the second question.

Thanks
Peter

"Rick Rothstein (MVP - VB)" wrote:

Yes, you can embed one function call in another (as long as the return
type
is compatible with what the parent function is expecting). In your
example,
you have this line...

Three = One / X

but one is declared like this...

Function One(X, Y, Z)

so you have to specify the arguments when you call it. Your assignment
line
will have to look like this..

Three = One(Var1, Var2, Var3) / X

where Var1, Var2 and Var3 have scope within your Three function. I
thought
the idea behind your asking about using the Global variables was so that
you
wouldn't have to do this?

Rick


"Palpha32" wrote in message
...
Thanks Rick, that works well.....except where I use function one in
another
function eg.

function three(x)
three = one / x
end function

is this possible?

Peter

"Rick Rothstein (MVP - VB)" wrote:

If you declare your variables in the (General)(Declarations) section
of
the
code module where your function are (that is, declare them outside of
any
procedures), then they will be "global" to the code module and **any**
procedure within that code module will be able to read AND CHANGE
them.
That
"and change them" is very important, especially if you do not declare
all
your variables since then you could accidentally use the same variable
name
in an unrelated procedure, change the value there without realizing
the
effect doing so will have elsewhere and thus screw up some other
procedure
that is dependent on those variables. If you give your global
variables
some
distinctive feature (say, start each name with "global"), then the
odds
of
accidentally tripping over them is greatly reduced. Now, with all that
said,
here is an example...

Dim A As Long
Dim B As Long

Function One(X, Y, Z)
A = X + Y
B = X + Z
One = A * B
End Function

Function Two()
Two = A / B
End Function

Rick


"Palpha32" wrote in message
...
One of my custom functions calculates certain values("a" and "b")
and
then
calculates a result relying on "a" and "b". Am I able to use "a"
and
"b"
in
another function by getting their value from the first function?

function one(x,y,z)
a = x + y
b = x + z
c = a * b
end function

function two(x,y,z)
a = x + y
b = x + z
d = a / b
end function

Thanks
Peter








All times are GMT +1. The time now is 10:39 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com