#1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 14
Default 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
  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 1
Default 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


  #3   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 14
Default 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



  #4   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 1
Default 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




  #5   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 14
Default 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






  #6   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 1
Default 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





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom functions GKW in GA Excel Discussion (Misc queries) 3 February 20th 08 02:16 PM
Custom Functions DTTODGG Excel Worksheet Functions 7 January 11th 08 07:37 PM
Custom Functions Sloth Excel Discussion (Misc queries) 5 July 25th 06 04:59 PM
Using custom functions within custom validation Neil Excel Discussion (Misc queries) 4 December 14th 05 10:40 PM
Custom Functions scott Excel Worksheet Functions 2 December 28th 04 12:23 AM


All times are GMT +1. The time now is 11:49 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"