global variables - shared subroutines

M

Mirka

I have the following problem, I have two modules, that means two macros, in
my application and there are some subroutines which are common and I was
trying to have them in only one module and to call them by module1.sub_name.
These subs change the value of a global variable which has purposely the same
name in both modules but I noticed that when I call them by that way the
value of the global variables do not change.

Does anyone know if I am doing something wrong or why is this happening? It
should be another way except of copying the subroutines in both modules,
right?
 
J

Jonathan West

Hi Mirka,

Declare the global variable in only one place. Declare it before the first
routine in a module, and use the Public keyword. Make sure that there are no
duplicate declarations anywhere.

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
M

Mirka

Thank you Jonathan for your reply.

I followed your steps -I haven't thought about the public declaration of the
variable- but once again the value of my global variable does not change when
I call the sub from the other module.
 
J

Jonathan West

Can you show me the code you are using, both to declare the variable and to
change it? Indicate the exact name and type of module where both the
variable declaration and calling code are located.

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
M

Mirka

I have two modules in the folder 'modules' of my project, module1 and module2.

I declare my variables 'version', 'inMaster' and 'inDetails' at the
beginning of module1 before my first function by this way: public version as
integer, public inMaster as boolean and public inDetails as boolean. Then in
module1 again I have the following sub:

Public Sub Determine_Location_Version(ByVal xNode As MSXML2.IXMLDOMNode)

' We get out from "Master" but not out of "Body"
If inMaster = True And xNode.ParentNode.nodeName = "Body" Then
inMaster = False
End If

' We get in "Master"
If xNode.nodeName = "Master" Then
inMaster = True
' Check which xml version we have
If xNode.HasChildNodes Then
For i = 0 To xNode.ChildNodes(0).ChildNodes.Length - 1
If xNode.ChildNodes(0).ChildNodes(i).nodeName = caption Then
version = 2
End If
Next
End If
End If

' We get out from "Details" but not out of "Body"
If inDetails = True And xNode.ParentNode.nodeName = "Body" Then
inDetails = False
End If

' We get in "Details"
If xNode.nodeName = "Details" Then
inDetails = True
End If

End Sub


In module2 I do not declare these 3 variables once again as you told me and
I call the sub somewhere in my code by: module1.Determine_Location_Version
xNode.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top