ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Calling Public Variables between diff Modules (https://www.excelbanter.com/excel-programming/392668-calling-public-variables-between-diff-modules.html)

J@Y

Calling Public Variables between diff Modules
 
I declared a public variable "Lib" in Module1. I have a For loop in Module 1
using "Lib" :

For Lib = 1to 10

....(code) ...

Next

In the (code) section, I call Module2. When Module2 calls the Lib variable,
gives me the ambiguous variable error. Is this because the For statement
actually declares Lib as a procedural variable instead of using it as a
public variable?

Don Guillett

Calling Public Variables between diff Modules
 
You might try changing
for lib to for i

--
Don Guillett Excel MVP
SalesAid Software

"J@Y" wrote in message
...
I declared a public variable "Lib" in Module1. I have a For loop in Module
1
using "Lib" :

For Lib = 1to 10

...(code) ...

Next

In the (code) section, I call Module2. When Module2 calls the Lib
variable,
gives me the ambiguous variable error. Is this because the For statement
actually declares Lib as a procedural variable instead of using it as a
public variable?



Chip Pearson

Calling Public Variables between diff Modules
 
As long as Lib is declared only in one module as a Public variable, you
should be able use it from any other procedure in any module. Make sure you
are not declaring it anywhere else. Also, "Lib" is a reserved word in VB
(used with the Declare statement). I would rename the variable to avoid
confusion.


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


"J@Y" wrote in message
...
I declared a public variable "Lib" in Module1. I have a For loop in Module
1
using "Lib" :

For Lib = 1to 10

...(code) ...

Next

In the (code) section, I call Module2. When Module2 calls the Lib
variable,
gives me the ambiguous variable error. Is this because the For statement
actually declares Lib as a procedural variable instead of using it as a
public variable?



JMB

Calling Public Variables between diff Modules
 
It sounds like a name conflict (perhaps Lib is declared more than once?).

Also- as Don suggests, I would not use a public variable as a control for a
For loop. Reason is if the second Sub modifies the variable it messes up
your For loop. Even if you try using parameter passing:


Public Lib As Long

Sub test()
For Lib = 1 To 10
Call Test2(Lib)
Next Lib
End Sub

Sub Test2(x As Long)
x = x + 1
Debug.Print Lib
End Sub


VBA, by default, passes parameters By Reference (ByRef). In the above
example, x is another name for Lib - so any change to x changes Lib. You can
pass By Value (ByVal) to avoid this, but I would avoid using referencing my
control variable in any other sub.


"J@Y" wrote:

I declared a public variable "Lib" in Module1. I have a For loop in Module 1
using "Lib" :

For Lib = 1to 10

...(code) ...

Next

In the (code) section, I call Module2. When Module2 calls the Lib variable,
gives me the ambiguous variable error. Is this because the For statement
actually declares Lib as a procedural variable instead of using it as a
public variable?



All times are GMT +1. The time now is 08:42 AM.

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