Global (Public) Variable Help

screech

Active Member
Joined
May 27, 2004
Messages
296
I have read a lot of help files and posts about global variables. I think I am doing this correctly but I continue to get the "ambiguous name" error. In short, I have several modules of code that each do something different. I am attempting to write another routine in another module to link all of these other routines in separate modules together. Naturally, I want to pass a defined variable from one routine in one module to another routine in another module. Declaring a variable as Public at the top of the first procedure should work, but that error keeps coming up.

This is a over-simplified breakdown of my code:

'' Module 1
Option Explicit
Public SheetName as String
Sub Macro1()
SheetName = Sheet1
Call Macro2
Call Macro3
End Sub

'' Module 2
Sub Macro2
[Using SheetName as a variable produces an error, it's been redefined as ""]
End Sub

'' Module 3
Sub Macro3
[Will use SheetName but never get this far as code breaks]

I have read somewhere that having code that opens another sheet, pulls data, and then closes it can clear out global variables. Is this what is happening here? Are there other options available to keep the variable defined across multiple routines in multiple modules?

Thanks for all the guidance!
 
I appreciate both responses, Erik and Tushar.

I discovered that if I typed "Option Explicit" right before I dimensioned the global variables that that solved my problem.
 
Upvote 0

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

Forum statistics

Threads
1,214,976
Messages
6,122,539
Members
449,088
Latest member
RandomExceller01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top