ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   global variable not seen (https://www.excelbanter.com/excel-programming/392665-global-variable-not-seen.html)

scott

global variable not seen
 
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".


Dave Peterson

global variable not seen
 
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





Scott wrote:

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".


--

Dave Peterson


All times are GMT +1. The time now is 08:12 PM.

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