Assign inputs from userform1.textbox1 entries as variables

bugatti79

Board Regular
Joined
Feb 18, 2012
Messages
70
Folks,

I have a userform with 2 textbox objects defined for the user to enter some numbers. How do I assign these entries as variables for further calculations?
For example: The 2 entries are 'max value' and 'min value'
I want to calculate the 'difference' Here is some code but not correct obviously. (All these snippets are on the userform code)

Code:
Private Sub TextBox1_Change()
'First entry
MyVal1 = UserForm1.TextBox1.Value
End Sub

Code:
Private Sub TextBox1_Change()'second entry
MyVal2 = UserForm1.TextBox2.Value
End Sub

Code:
Public Function Calculations()
'calculate difference
Cells(1, 1) = MyVal1 - MyVal2
End Function
The above snippet I don't necessarily want it in a cell but assign it to another variable
Thanks
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Where have you declared MyVal1 and MyVal2?

If you want the result in a variable perhaps you could try this.
Code:
Private Sub TextBox1_Change()
   MsgBox Calculations
End Sub


Private Sub TextBox1_Change()
   MsgBox  Calculations
End Sub

Public Function Calculations()
'calculate difference
    Calculations = Val(TextBox1.Value) = Val(TextBox2.Value)
End Function
 
Upvote 0
I tried that, it still doesnt work. Also, I do not need the message box to appear as I just want to enter the numbers into the textBox object.

I changed my code to declare the values MyVal1 and 2

Code:
Private Sub TextBox1_Change()
Dim MyVal1 As Single
MyVal1 = UserForm1.TextBox1.Value
End Sub

Code:
Private Sub TextBox2_Change()
Dim Diff As Single, MyVal2 As Single
MyVal2 = UserForm1.TextBox2.Value
Diff = MyVal2 - MyVal1
Cells(1, 1) = Diff
End Sub

It seems to be entering the value of MyVal2 into the cell and not the difference above....?
 
Upvote 0
When I run through first sub procedure to stores the entered value as MyVal1 but when I run the second procedure the value of MyVal1 goes back to 0 and thus the value displayed in the cell will just be MyVal2. How do I retain the MyVal1 value for the second procedure?
 
Upvote 0
The variable MyVal1 is not recognised outside the sub it's been declared in.

Do you need variables anyway?
Code:
Private Sub TextBox2_Change()
    Me.TextBox2.Value = Val(TextBox2.Value) -  Val(UserForm1.TextBox1.Value)
End Sub

PS The message box was just to show what was happening.
 
Upvote 0
The variable MyVal1 is not recognised outside the sub it's been declared in.

Do you need variables anyway?
Code:
Private Sub TextBox2_Change()
    Me.TextBox2.Value = Val(TextBox2.Value) -  Val(UserForm1.TextBox1.Value)
End Sub

PS The message box was just to show what was happening.
Do i declare all variables at the top before all procedures?
Why have you used 'textbox2' twice in the above line?
 
Upvote 0
The code I posted doesn't use variables.

I used TextBox2 because my finger slipped off the 3 and hit 2, it should be TextBox3 to the left of =.
 
Upvote 0
The code I posted doesn't use variables.

I used TextBox2 because my finger slipped off the 3 and hit 2, it should be TextBox3 to the left of =.

ok. The userform has a number of private sub procedures, I changed them all to public so that I could declare the variable at the very top before all the procedures. It doesnt seem to work. It only works when it is declared within the procedure with the calculation...any thoughts?
 
Upvote 0
What exactly are you trying to do?
 
Upvote 0

Forum statistics

Threads
1,213,490
Messages
6,113,956
Members
448,535
Latest member
alrossman

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top