PDA

View Full Version : Problem decimal calculation in VBA



jmaocubo
11-02-2011, 09:02 AM
Hi!!!!

I have a userform with two TextBox. The aim is to divide the value of textbox1 with textbox2.
I am using the following code:

Private Sub CommandButton1_Click()
' P2
If TextBox1.Value <> "" And TextBox2.Value <> 0 Then

Sheets("Auxiliar").Range("H2").Value = TextBox1.Value
Sheets("Auxiliar").Range("I2").Value = TextBox2.Value


Sheets("Auxiliar").Range("D2").Value = (TextBox1.Value / TextBox2.Value)

TextBox1.Value = ""
TextBox2.Value = ""


End If


UserForm2.Hide

End Sub

So far so good .... the problem is if you use "." or "," as decimal separator.

ex: 1.2 /1 i get 12
using 1,2 / 1 i get 1,2

Is there any way the calculation is the same with "." or ","?

Thanks in advance

Miguel

Rob342
11-02-2011, 04:10 PM
Miguel

Off the cuff, if you are using decimals in Both txtboxes
In userform initialize.

Me.Txtbox1.Value=Format(0#, "##0.00")
Me.Txtbox2.Value=Format(0#, "##0.00")


Amend your code and add Me. before the Txtbox1.value for eg


Private Sub CommandButton1_Click()
' P2 If Me.TextBox1.Value <> "" And Me.TextBox2.Value <> 0 Then
Sheets("Auxiliar").Range("H2").Value = Me.TextBox1.Value
Sheets("Auxiliar").Range("I2").Value = Me.TextBox2.Value
Sheets("Auxiliar").Range("D2").Value = (Me.TextBox1.Value / Me.TextBox2.Value)
Me.TextBox1.Value = ""
Me.TextBox2.Value = ""
End If
UserForm2.Hide
End Sub

jmaocubo
11-02-2011, 04:19 PM
Thanks Rob

i'm using the code:
Private Sub CommandButton1_Click()
' P2
If TextBox1.Value <> "" And TextBox2.Value <> 0 Then

Sheets("Auxiliar").Range("H2").Value = Replace(TextBox1.Value, ",", ".")
Sheets("Auxiliar").Range("I2").Value = Replace(TextBox2.Value, ",", ".")




Sheets("Auxiliar").Range("D2").Value = (Replace(TextBox1.Value, ".", ",") / Replace(TextBox2.Value, ".", ","))

TextBox1.Value = ""
TextBox2.Value = ""


End If


UserForm2.Hide

End Sub



:beerchug: