Transfer variable between macros

W

WLMPilot

I am using a userform to create an order for stock. Variables are used
during initialization, ie stklvl = whatever the max stock level is for that
item.

The user then enters the quantity on hand. When the user presses the enter
key, the following macro executes:

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)
If KeyCode = 13 Or KeyCode = 9 Then
TextBox2 = Str(stklvl) - Str(TextBox1)
End If
End Sub


Textbox2 is the difference between max stock level (stklvl) and quantity on
hand.

How do I transfer the value of stklvl from the initialization macro to this
macro so I can compute the difference?

Thanks,
Les
 
S

Sam Wilson

You need to have stklvl declared as a public variable.

If you currently have the line "Dim stklvl" in the initialisation macro,
delete it. Then, before that macro type:

Public stklvl as Variant
 
M

Mike H

Hi,

Declare the variable as public, set syklvl in the initialise code and it
will pass to your keydown code

Public stklvl As Long
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)
If KeyCode = 13 Or KeyCode = 9 Then
TextBox2 = stklvl - Val(TextBox1)
End If
End Sub


Private Sub UserForm_Initialize()
stklvl = 99
End Sub


Mike
 
W

WLMPilot

Thanks, will give it a shot



Sam Wilson said:
You need to have stklvl declared as a public variable.

If you currently have the line "Dim stklvl" in the initialisation macro,
delete it. Then, before that macro type:

Public stklvl as Variant
 
W

WLMPilot

Thanks, will give it a shot

Mike H said:
Hi,

Declare the variable as public, set syklvl in the initialise code and it
will pass to your keydown code

Public stklvl As Long
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)
If KeyCode = 13 Or KeyCode = 9 Then
TextBox2 = stklvl - Val(TextBox1)
End If
End Sub


Private Sub UserForm_Initialize()
stklvl = 99
End Sub


Mike
 

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