Consulting

Results 1 to 3 of 3

Thread: Problem decimal calculation in VBA

  1. #1
    VBAX Regular
    Joined
    Aug 2009
    Posts
    69
    Location

    Problem decimal calculation in VBA

    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:

    [vba]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[/vba]

    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

  2. #2
    VBAX Mentor
    Joined
    Apr 2009
    Location
    Kingsbury
    Posts
    423
    Location
    Miguel

    Off the cuff, if you are using decimals in Both txtboxes
    In userform initialize.
    [vba]
    Me.Txtbox1.Value=Format(0#, "##0.00")
    Me.Txtbox2.Value=Format(0#, "##0.00")
    [/vba]

    Amend your code and add Me. before the Txtbox1.value for eg
    [vba]
    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
    [/vba]

  3. #3
    VBAX Regular
    Joined
    Aug 2009
    Posts
    69
    Location
    Thanks Rob

    i'm using the code:
    [VBA]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[/VBA]




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •