PDA

View Full Version : Variable scope problem



tqm1
05-28-2007, 02:47 AM
Ref: http://www.excelforum.com/showthread.php?t=601456

Dear Experts

I assigned a Public Variable "code" on General Declaration of sheet1 as


public code as long

There is a command button, on Userform, with following codes


code = Application.InputBox("Enter value for code")
Unload Me

I write 100 into Inputbox.

When I recal the value of variable code, it shows empty instead of 100.

Why userform inputbox value does not come into variable CODE?
What is wrong?

I want to create some variable, that should be recal anywhere in

workbook or Userform.

So where I should assign variable and what type?

Please help

Bob Phillips
05-28-2007, 03:53 AM
Your workbook doesn't actually have a variable called code declared at all, but anyway it should be declared Public in the standard code module, not a worksheet code module.

PS Evaluate won't work as you do it.

Bob Phillips
05-28-2007, 03:56 AM
Just to add, It will lose its value anyway if you look at it from a separate button. And why do you invoke an inpoutbox from a userform, why not add a textbox to the form?

tqm1
05-28-2007, 08:30 AM
Dear Sir,

I could not get the solution of my problem.
Why varible loses its value?

I repeat my question:

I want to create some variable, that should be recal anywhere in
workbook or Userform.

Please help again

Bob Phillips
05-28-2007, 09:19 AM
And I repeat ... but anyway it should be declared Public in the standard code module, not a worksheet code module.

Paul_Hossler
05-28-2007, 04:27 PM
Not sure what you want to do, but could you work with this bit of VBA? Make sure to enter them onto a module under Modules, NOT Excel Objects or Forms.

Good luck

Paul


Option Explicit
Public code As Long
Sub Display_Code()
MsgBox "The value for code = " & code
End Sub
Sub Enter_Code()
code = InputBox("Please Enter Code", "Code")
End Sub