In Excel VBA you can declare variables or not, if they do not declare the program automatically takes them as being of type variant. In this way a variable can take different values \u200b\u200bfrom integers (integer), string (string), date (date), etc, but due to their status as "variant" will take up more memory. That is not desirable .
To avoid this problem, you can use the Option Explicit statement at the beginning of the module, this forces us to declare all the variables we use. Now if you want to use a variable in a subroutine (Sub) or functions (Function), How to declare?
Using the Option Explicit statement, we must declare each of the variables. For example if we have the following sub:
Sub A () Dim x as integer
x = 2 * x-1
End Sub Sub Main
And calling
A Sub Sub Main () Dim x
as integer x = 10
Call A
End Sub Msgbox x
Main
Then when you run, the message will be the value of 10, and that despite the Sub is called A and it works with a variable x, the scope of this x is only within the Sub A and the back end control over the x declared in Main. The moral of this es que cuando se declara como Dim o Private dichas variables sólo son válidas dentro de la subrutina o función donde se crearon.
Ahora si se declarase de la siguiente manera:
Option explicit
Public x as integer
Sub A()
x = 2*x-1
End Sub
Y el Sub Main que llama a Sub A
Sub Main()
x = 10
Call A
Msgbox x
End Sub
Luego al ejecutar Main, el mensaje será 19, puesto que la variable x al ser declarada como Public o pública se puede usar dentro de cualquier subroutine and / or function.
I hope it was helpful. But it was so they can make their inquiries.
0 comments:
Post a Comment