Thursday, September 2, 2010

Older Curves Galleries

Functions and Subroutines in VBA Excel

In excel VBA, the two procedures used are the functions and subroutines. Differences are mainly in the subroutines returns not only perform operations on a data set or perform a particular action, while the functions if they return a value or result, which is why when you declare a function to be held that such variable is the value returns.

excel A VBA function is declared as:
[Public or Private] Function funcname (as tipodato1 Param1, Param2 as

tipodato2 ,...) where the terms in brackets is optional, if nothing is written function is taken public and this can be used anywhere in VBA excel module. For example, defining the following function:


RaizSuma
Then the function calculates the square root of the numbers "x" e "and" when calling the function must be integers, if appropriate parameters are entered by calling the function will result in error. You may also notice that the result of this function is a value of type Double as many decimal places will probably. Also note that the return value is assigned to the name of the function in this case RaizSuma. You can load this example in an Excel VBA module and then call a cell in excel to try how it works. To use a function within another function or subroutine, it is just for example:

x = RaizSuma (5.4) 'x must be of type double so that no error


A subroutine is declared similarly:
[Public or Private] Sub NombreSubrutina (as tipodato1 Param1, Param2 as tipodato2, ...)


In this case we see that the subroutine does not return any particular value, if in it you use a variable defined as public you make changes on this, but does not return any results. Most commonly used to perform actions, for example



A Sub is called as follows: Call

Post (12)

will return the message: "Value increased or equal to ten. "

I hope you have served. Any questions let us know.

0 comments:

Post a Comment