ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Sub(), Modules, and Private/ Global variables (https://www.excelbanter.com/excel-programming/391889-sub-modules-private-global-variables.html)

J@Y

Sub(), Modules, and Private/ Global variables
 
I trying to understand is Modular programming thing. What exactly does sub()
define? How would I create variables which can be called upon by different
modules?

Chip Pearson

Sub(), Modules, and Private/ Global variables
 
There two types of procedures (sections of code that perform an action): Sub
procedures and Function procedure. The difference between them is that
Function procedure can return a value as the result of the function, whereas
Sub procedure do not return any value. E.g.,

Function XZY(D As Double) As Double
XYZ = D * 2
End Function

Procedure XYZ returns D * 2 as its result which can be assign to another
variable:

Dim A As Double
A = XYZ(4)

Subs don't return value. Therefore,

Sub ABC(D)
' code
End Code

is valid but

Dim A As Double
A = ABC()

is invalid because ABC can't return a value.

If you need to pass data around between procedure, you can declare variable
input parameters (e.g,. D As Double) in either a Sub or Function declaration
and those parameter values can used within the procedure. You can also
define variable at the module or project level by declaring them before and
outside of any procedure. E.g.,

Public X As Double
Sub FirstSubInModule()
' code
End Sub

Here, X will be visible throughout the project. This is refered to as the
"scope" of a variable and is document in the on line help.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)


"J@Y" wrote in message
...
I trying to understand is Modular programming thing. What exactly does
sub()
define? How would I create variables which can be called upon by different
modules?



J@Y

Sub(), Modules, and Private/ Global variables
 
So one project is a single module? you can't have a variable shared between
say Module1 and Module2?

"Chip Pearson" wrote:

There two types of procedures (sections of code that perform an action): Sub
procedures and Function procedure. The difference between them is that
Function procedure can return a value as the result of the function, whereas
Sub procedure do not return any value. E.g.,

Function XZY(D As Double) As Double
XYZ = D * 2
End Function

Procedure XYZ returns D * 2 as its result which can be assign to another
variable:

Dim A As Double
A = XYZ(4)

Subs don't return value. Therefore,

Sub ABC(D)
' code
End Code

is valid but

Dim A As Double
A = ABC()

is invalid because ABC can't return a value.

If you need to pass data around between procedure, you can declare variable
input parameters (e.g,. D As Double) in either a Sub or Function declaration
and those parameter values can used within the procedure. You can also
define variable at the module or project level by declaring them before and
outside of any procedure. E.g.,

Public X As Double
Sub FirstSubInModule()
' code
End Sub

Here, X will be visible throughout the project. This is refered to as the
"scope" of a variable and is document in the on line help.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)


"J@Y" wrote in message
...
I trying to understand is Modular programming thing. What exactly does
sub()
define? How would I create variables which can be called upon by different
modules?



Chip Pearson

Sub(), Modules, and Private/ Global variables
 
So one project is a single module? you can't have a variable shared
between
say Module1 and Module2?


A project can contain as many modules as required. In large projects I do,
have many modules that group together various procedures by functionality. I
also have a library of about 100 modules of canned, pre-tested code that I
selectively include in projects where certain functionality is needed. If
you want to access the same variable in 2 or or more module, declare the
variable using "Public". E.g.

Public Z As Double

The variable Z will be visible to all functions in all modules of the
project, assuming no other variable has the same name.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)


"J@Y" wrote in message
...
So one project is a single module? you can't have a variable shared
between
say Module1 and Module2?

"Chip Pearson" wrote:

There two types of procedures (sections of code that perform an action):
Sub
procedures and Function procedure. The difference between them is that
Function procedure can return a value as the result of the function,
whereas
Sub procedure do not return any value. E.g.,

Function XZY(D As Double) As Double
XYZ = D * 2
End Function

Procedure XYZ returns D * 2 as its result which can be assign to another
variable:

Dim A As Double
A = XYZ(4)

Subs don't return value. Therefore,

Sub ABC(D)
' code
End Code

is valid but

Dim A As Double
A = ABC()

is invalid because ABC can't return a value.

If you need to pass data around between procedure, you can declare
variable
input parameters (e.g,. D As Double) in either a Sub or Function
declaration
and those parameter values can used within the procedure. You can also
define variable at the module or project level by declaring them before
and
outside of any procedure. E.g.,

Public X As Double
Sub FirstSubInModule()
' code
End Sub

Here, X will be visible throughout the project. This is refered to as the
"scope" of a variable and is document in the on line help.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)


"J@Y" wrote in message
...
I trying to understand is Modular programming thing. What exactly does
sub()
define? How would I create variables which can be called upon by
different
modules?




[email protected][_2_]

Sub(), Modules, and Private/ Global variables
 
I could spend an hour writing up what it means - but you probably
won't bother to read what I'd write - you could just a easily go to a
Borders store and read up on it in an Excel book there.

My suggestion is that you Google "Module vs Class Module Options"

Fear not, Public and Private are words Excel understands.




All times are GMT +1. The time now is 03:20 PM.

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