global variable not seen

S

Scott

I have a simple workbook with 1 sheet and a command button to run some code.
Under "Microsoft Excel Objects", Sheet1 I have this code:

Public Y As String
Public Sub CommandButton1_Click()
X = "passed parameter"
Y = "global variable"
testmodule (X)
End Sub

Under "Modules", Module1 I have this code:

Public Sub testmodule(X As String)
MsgBox ("X is " + X)
MsgBox ("Y is " + Y)
End Sub

When I press the command button I see two message boxes. The first says :

"X is passed parameter"

and the second says

"Y is "

Question is how can I get the Y variable seen in Module1 without passing it
as a parameter? In other words have the message box say "Y is global
variable".
 
D

Dave Peterson

Try moving
Public Y As String
to the top of the General module.

Or if you want to keep Y declared in the Sheet1 worksheet module:

MsgBox "Y is " + Sheet1.Y
 

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