I am trying to create a global variable in a user form within Microsoft Excel.
I know the variable must be in the General area. So I type:
bt = Sheets(10).Range("G25").value
then I click play(run), but that highlights "G25" and says :
Compile Error:
Invalid outside procedure
I tried just typing:
bt="hello"
And then I hit play(run), and it highlighted "hello" and gave me the same error.
What am I doing wrong

Trying to create a global variable
Radiant-designer
Hi Anas,
You can't set constants to a dynamic value, constant values are set once when they are created and that value cannot change at all during the program. With your code there your setting the constant to a cells value and that cell's value could change. The value isn't a constant.
What to do is create a property in your module called bt which returns the value from the sheets cell.
Public Property Get bt() As String
bt = CStr(Sheets(1).Range("g25").Value)
End Property
Public Property Let bt(ByVal vNewValue As String)
Sheets(1).Range("g25").Value = vNewValue
End Property
This lets you use the value like a global variable "Globals.bt"
Hanspeter
Hey Derek,
I figured that part out last night, but I did not need a variable that would work outside of a userform, just outside the "private sub". So in the module i created a line:
public const bt as string = Sheets(1).Range("g25").value
But I dont think Sheets(1).Range("g25").value is a string. So i made it an object. So what the debugger does is highlight public const saying it is an incorrect way to declare an object.
Is the value a string, an object, or something other than that And how do i declare it
joynerCN
Hello Anas.
You need to define global variables in a module and they need to be public.
Public myGlobalString as String
Variables placed in user forms are created and destroyed as the form is displayed and then closed again so their scope cannot be global.