Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
J@Y J@Y is offline
external usenet poster
 
Posts: 127
Default 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?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default 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?


  #3   Report Post  
Posted to microsoft.public.excel.programming
J@Y J@Y is offline
external usenet poster
 
Posts: 127
Default 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?


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default 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?



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


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
Global variables Ed Excel Programming 4 August 7th 06 04:00 AM
Global Variables Francis Brown Excel Programming 9 November 27th 05 06:14 PM
Private modules and global variables davidm Excel Programming 6 November 25th 05 07:10 PM
Public, Private, Event modules, Forms modules,,, Jim May Excel Programming 11 October 31st 05 03:12 AM
public but private variables No Name Excel Programming 2 May 19th 04 12:41 PM


All times are GMT +1. The time now is 04:53 AM.

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"