PC Review


Reply
Thread Tools Rate Thread

Calling Public Variables between diff Modules

 
 
=?Utf-8?B?SkBZ?=
Guest
Posts: n/a
 
      4th Jul 2007
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?
 
Reply With Quote
 
 
 
 
Don Guillett
Guest
Posts: n/a
 
      4th Jul 2007
You might try changing
for lib to for i

--
Don Guillett Excel MVP
SalesAid Software
(E-Mail Removed)
"J@Y" <(E-Mail Removed)> wrote in message
news:B12B77B5-9B30-4AF8-93FA-(E-Mail Removed)...
>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?


 
Reply With Quote
 
Chip Pearson
Guest
Posts: n/a
 
      4th Jul 2007
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" <(E-Mail Removed)> wrote in message
news:B12B77B5-9B30-4AF8-93FA-(E-Mail Removed)...
>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?


 
Reply With Quote
 
=?Utf-8?B?Sk1C?=
Guest
Posts: n/a
 
      4th Jul 2007
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?

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Public Variables, Class Modules and Standard Modules Excel Monkey Microsoft Excel Programming 2 30th Apr 2009 03:18 PM
Recommendation on calling module with public variables laavista Microsoft Access 2 17th Apr 2009 05:13 AM
Public, Private, Event modules, Forms modules,,, Jim May Microsoft Excel Programming 11 31st Oct 2005 03:12 AM
Public variables not holding value between modules Gail Microsoft Access VBA Modules 2 9th Jun 2004 07:20 PM
Re: Modules, forms and public variables Russ Microsoft Dot NET 0 1st Aug 2003 12:19 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:23 PM.