Reputation: 51
I have a userform, lets say when I click the button name in the userform it will request me to enter a name and it is always stored in
private sub button_click()
name = inputbox("Please enter your name:")
end sub
The thing I want is, when I start the macro first user form will appear then after user enters the necessary information, the variables values' will last until the end of the main macro.
I am searching for last two days couldn't find a solution but who knows maybe I couldn't find it. Therefore, i am writing here. Thanks in advance!
` another question related:
lets say I have
Public plant As Variant
Public checking As Variant
Public MeanCov1 As Variant
Public MeanCov2 As Variant
Public MeanCov3 As Variant
Public WeekCov1 As Variant
Public WeekCov2 As Variant
Public WeekCov3 As Variant
Public RolLow As Variant
Public RolHigh As Variant
Public ServiceLevel As Variant
Public TrendMultiplier As Variant
Public WeekCov11 As Variant
Public WeekCov22 As Variant
Public WeekCov33 As Variant
Sub initialize()
MeanCov1 = 0.3
MeanCov2 = 0.5
MeanCov3 = 0.7
WeekCov1 = 18
WeekCov2 = 13
WeekCov3 = 8
WeekCov11 = -14 + WeekCov1
WeekCov22 = -14 + WeekCov2
WeekCov33 = -14 + WeekCov3
RolLow = 0.3
RolHigh = 0.7
TrendMultiplier = 4
ROL_Analysis.Show
Application.Run ("ROL_Analysis_Macro")
End Sub
I can not initialize the public variables, for example in user form it says show variable values but when I click it is completely empty
Private Sub ROLparameter_Click()
Line1: RolLow = InputBox("Please enter the lower bound percentage for ROL calculation between 0 and 100 (initially " & RolLow & "%):")
If Not 0 <= RolLow <= 100 Then GoTo Line1
Line2: RolHigh = InputBox("Please enter the upper bound percentage for ROL calculation between 0 and 100 (initially " & RolHigh & "%):")
End Sub
Upvotes: 0
Views: 3005
Reputation: 2355
As was said, define a variable globally to access it in different subs and different modules. I have a very simple example here for you. I created a module called Module1. Here is all its code:
Option Explicit
Public name As String
Sub Test()
name = "David"
UserForm1.Show
MsgBox "The userform has closed"
End Sub
I also created a userform called UserForm1. Here is all the code and a screenshot of it:
Option Explicit
Private Sub CommandButton1_Click()
MsgBox Module1.name
End Sub
How it works:
If I launch Module1, the public
variable name
gets set to "David". In your case you could set it to whatever you want with your inputbox. After that, the userform is launched with .Show
and the Module1 stops reading code until you're done with UserForm1.
While Userform1 is active, clicking the button will open a MsgBox saying "David", because I access the public variable Module1.name
. I always specify which module the public variable comes from. When I close the userform, Module1 gets activated again and I get a msgbox saying "The userform has closed".
Does this answer all your questions?
Clicking the button:
Closing the form:
Upvotes: 1
Reputation: 845
To access a variable in a different sub, you define it globally
Public Name As String
Private Sub button_click()
Name = InputBox("Please enter your name:")
End Sub
Sub Foo()
MsgBox Name
End Sub
and for your second question, if you are clicking on the buttons, then for the below code you will get a alert if you have clicked the button which corresponds to the name you typed in the InputBox
Public name As String
Sub button_click()
name = InputBox("Please enter your name:")
End Sub
Private Sub button1_click()
If name = "john" Then
MsgBox name
End If
End Sub
Private Sub button2_click()
If name = "james" Then
MsgBox name
End If
End Sub
Private Sub button3_click()
If name = "david" Then
MsgBox name
End If
End Sub
Upvotes: 2