ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   How to write a VBA function which returns an integer (https://www.excelbanter.com/excel-programming/443502-how-write-vba-function-returns-integer.html)

Andrew[_56_]

How to write a VBA function which returns an integer
 
Hello,
I could use some help on the syntax of writing a function file in VBA
which takes two integers as input arguments and returns one integer.
Let's say my function was called TEST, and TEST took the average of
two integers.

It seems that the code should look like this:

AVG=TEST(2,8)

But all function files that i have seen in VBA use Call to execute the
function. Can someone help me out with this?

thanks
Andy


Don Guillett Excel MVP

How to write a VBA function which returns an integer
 
On Aug 13, 7:51*am, Andrew wrote:
Hello,
I could use some help on the syntax of writing a function file in VBA
which takes two integers as input arguments and returns one integer.
Let's say my function was called TEST, and TEST took the average of
two integers.

It seems that the code should look like this:

AVG=TEST(2,8)

But all function files that i have seen in VBA use Call to execute the
function. *Can someone help me out with this?

thanks
Andy


Function mavg(x, y)
mavg = (x + y) / 2
End Function
But why not just use =avg

Andrew[_56_]

How to write a VBA function which returns an integer
 
On Aug 13, 7:00*am, Don Guillett Excel MVP
wrote:
On Aug 13, 7:51*am, Andrew wrote:

Hello,
I could use some help on the syntax of writing a function file in VBA
which takes two integers as input arguments and returns one integer.
Let's say my function was called TEST, and TEST took the average of
two integers.


It seems that the code should look like this:


AVG=TEST(2,8)


But all function files that i have seen in VBA use Call to execute the
function. *Can someone help me out with this?


thanks
Andy


Function mavg(x, y)
*mavg = (x + y) / 2
*End Function
But why not just use =avg


I'm not trying to calculate the average, I'm trying to get an example
of how to make a function. So, consider this code where one
subroutine calls a function. This won't work. But how do I make it
work?

Sub fill_cells()
for k=1 to 10
cells(k,1)=mavg(k,5)
next
End Sub

Function mavg(x,y)
mavg=(x+y)/2
End Function

Don Guillett Excel MVP

How to write a VBA function which returns an integer
 
On Aug 13, 8:16*am, Andrew wrote:
On Aug 13, 7:00*am, Don Guillett Excel MVP
wrote:





On Aug 13, 7:51*am, Andrew wrote:


Hello,
I could use some help on the syntax of writing a function file in VBA
which takes two integers as input arguments and returns one integer.
Let's say my function was called TEST, and TEST took the average of
two integers.


It seems that the code should look like this:


AVG=TEST(2,8)


But all function files that i have seen in VBA use Call to execute the
function. *Can someone help me out with this?


thanks
Andy


Function mavg(x, y)
*mavg = (x + y) / 2
*End Function
But why not just use =avg


I'm not trying to calculate the average, I'm trying to get an example
of how to make a function. *So, consider this code where one
subroutine calls a function. *This won't work. *But how do I make it
work?

Sub fill_cells()
for k=1 to 10
cells(k,1)=mavg(k,5)
next
End Sub

Function mavg(x,y)
mavg=(x+y)/2
End Function- Hide quoted text -

- Show quoted text -


"If desired, send your file to dguillett @gmail.com I will only look
if:
1. You send a copy of this message on an inserted sheet
2. You give me the newsgroup and the subject line
3. You send a clear explanation of what you want
4. You send before/after examples and expected results."

Andrew[_56_]

How to write a VBA function which returns an integer
 
On Aug 13, 8:26*am, Don Guillett Excel MVP
wrote:
On Aug 13, 8:16*am, Andrew wrote:



On Aug 13, 7:00*am, Don Guillett Excel MVP
wrote:


On Aug 13, 7:51*am, Andrew wrote:


Hello,
I could use some help on the syntax of writing a function file in VBA
which takes two integers as input arguments and returns one integer..
Let's say my function was called TEST, and TEST took the average of
two integers.


It seems that the code should look like this:


AVG=TEST(2,8)


But all function files that i have seen in VBA use Call to execute the
function. *Can someone help me out with this?


thanks
Andy


Function mavg(x, y)
*mavg = (x + y) / 2
*End Function
But why not just use =avg


I'm not trying to calculate the average, I'm trying to get an example
of how to make a function. *So, consider this code where one
subroutine calls a function. *This won't work. *But how do I make it
work?


Sub fill_cells()
for k=1 to 10
cells(k,1)=mavg(k,5)
next
End Sub


Function mavg(x,y)
mavg=(x+y)/2
End Function- Hide quoted text -


- Show quoted text -


"If desired, send your file to dguillett I will only look
if:
1. You send a copy of this message on an inserted sheet
2. You give me the newsgroup and the subject line
3. You send a clear explanation of what you want
4. You send before/after examples and expected results."


In many programming languages having a function return a value is a
very fundamental construct. It is apparently not common in VBA,
otherwise it would not require an explanation any more than would an
assignment or a declaration. Thanks for your offer.

Andrew[_56_]

How to write a VBA function which returns an integer
 
On Aug 13, 9:32*am, Andrew wrote:
On Aug 13, 8:26*am, Don Guillett Excel MVP
wrote:



On Aug 13, 8:16*am, Andrew wrote:


On Aug 13, 7:00*am, Don Guillett Excel MVP
wrote:


On Aug 13, 7:51*am, Andrew wrote:


Hello,
I could use some help on the syntax of writing a function file in VBA
which takes two integers as input arguments and returns one integer.
Let's say my function was called TEST, and TEST took the average of
two integers.


It seems that the code should look like this:


AVG=TEST(2,8)


But all function files that i have seen in VBA use Call to execute the
function. *Can someone help me out with this?


thanks
Andy


Function mavg(x, y)
*mavg = (x + y) / 2
*End Function
But why not just use =avg


I'm not trying to calculate the average, I'm trying to get an example
of how to make a function. *So, consider this code where one
subroutine calls a function. *This won't work. *But how do I make it
work?


Sub fill_cells()
for k=1 to 10
cells(k,1)=mavg(k,5)
next
End Sub


Function mavg(x,y)
mavg=(x+y)/2
End Function- Hide quoted text -


- Show quoted text -


"If desired, send your file to dguillett I will only look
if:
1. You send a copy of this message on an inserted sheet
2. You give me the newsgroup and the subject line
3. You send a clear explanation of what you want
4. You send before/after examples and expected results."


In many programming languages having a function return a value is a
very fundamental construct. *It is apparently not common in VBA,
otherwise it would not require an explanation any more than would an
assignment or a declaration. * Thanks for your offer.


I don't think this can be done with one line, but two lines of code
will do it. Here's how this will work. Obviously this is a very
simple function, but this will work for more complicated functions as
well.

' Subroutine to call function
' ===================
Sub fill_cells()
for k=1 to 10 '
Call mavg(k,5) ' enter values of k and 5 to average function
mavg
X=mavg(k,5) ' X is the returned value. X has to be a
global declaration
next
End Sub

' Function to average two numbers
' ========================
Function mavg(x,y)
X = (x+y)/2
End Function


All times are GMT +1. The time now is 11:27 AM.

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