• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Code to calculate difference between textbox values

Rodrigues

Member
Hi
I'm would like to perform a calculation of the difference between two textbox on a userform (file attached) in percentage and as a whole figure.
I.e.: TextBox8 = textbox6 - textbox7 / textbox6

Example on userform attached:
textbox4 = 100
textbox5 = 10
textbox6 = 98%
textbox7 = 10% (does automatically a calculation)

So, textbox8 should be = 89.80%
Textbox9 as a whole number should be = 88
Tested without success codes attached.
Code:
Private Sub TextBox8_Change()
TextBox8.Value = Format(Val(TextBox6.Value) - Val(TextBox7.Value) / TextBox6.Value, "0.00%")
End Sub

Private Sub TextBox9_Change()
TextBox9.Value = Format(Val(TextBox6.Value) - Val(TextBox7.Value))
End Sub
Thanks R
 

Attachments

Hi !

By following your formula (textbox6 - textbox7 / textbox6)
result is 87.80%

As a TextBox value is a string, you have to convert each Textbox
(as you forgot to convert one is your code !) to a number
via CDbl function (or Val if dot is the decimal separator) for example
or Evaluate method (if % inside string for example, second error in your code)
Use Format function to round result in a TextBox.
Check result of each conversion via MsgBox or Debug.Print

Once understanding this very basics string versus number,
you can use a global Evaluate to directly catch the result :

EVALUATE%25.gif

 
All is yet in my previous post as well a sample in the animation !

Private Sub TextBox8_Change()
TextBox8.Value = Format(Val(TextBox6.Value) - Val(TextBox7.Value) / TextBox6.Value, "0.00%")
End Sub

As I wrote, you forgot to convert to numeric each TextBox !
 
Back
Top