+ Reply to Thread
Results 1 to 8 of 8

Wrong result adding values of text boxes in a userform.

Hybrid View

  1. #1
    Forum Contributor MariaPap's Avatar
    Join Date
    10-19-2013
    Location
    Chania-Crete-Greece
    MS-Off Ver
    Excel 2003-2010
    Posts
    319

    Wrong result adding values of text boxes in a userform.

    Hello

    I am not able to understand this.

    I have 4 text boxes in a user form where user type numbers. At the end i have another text box(yellow one) that adds these numbers using this code.
    Dim x As Integer, Tot As Double
        Me.TextBox5.Text = ""
        For x = 1 To 4
        Tot = Tot + Val(Me("prcase" & x).Text)
        Next x
        Me.TextBox5.Text = Tot
    In another text box(grey one) user types another number.

    Then using this code

    Private Sub TextBox6_Change()
    TextBox7.Text = Val(TextBox5.Text) + Val(TextBox6.Text)
    End Sub
    i try to add the values of the two previous text boxes.

    I get wrong result when i use decimals numbers and i can not understand what is wrong.

    Thanks in advance for any assistance here.
    Attached Files Attached Files

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Wrong result adding values of text boxes in a userform.

    What are you using as a decimal separator when you enter values?
    If posting code please use code tags, see here.

  3. #3
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Wrong result adding values of text boxes in a userform.

    decimals need to go as doubles, so dblValue1=cdbl(textbox1.text) and dblValue2=cdbl(textbox2.text)

    then tot will be the sum of the two doubles, tot will need to be a double to.

    I'd put in an ISNUMERIC() check on both boxes before running the sub.
    Hope this helps

    Sometimes its best to start at the beginning and learn VBA & Excel.

    Please dont ask me to do your work for you, I learnt from Reading books, Recording, F1 and Google and like having all of this knowledge in my head for the next time i wish to do it, or wish to tweak it.
    Available for remote consultancy work PM me

  4. #4
    Forum Contributor MariaPap's Avatar
    Join Date
    10-19-2013
    Location
    Chania-Crete-Greece
    MS-Off Ver
    Excel 2003-2010
    Posts
    319

    Re: Wrong result adding values of text boxes in a userform.

    We use COmma in Greece MR Norie.

    The first video is using nathansav's suggestion like this.
    Private Sub TextBox6_Change()
    TextBox7.Text = CDbl(TextBox5.Text) + CDbl(TextBox6.Text)
    End Sub
    http://screencast.com/t/05SPHBl9w

    and the other one as i mentioned in my first post

    http://screencast.com/t/OY9WKHrlJ

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Wrong result adding values of text boxes in a userform.

    The problem is the comma, if you use a period (.) for the separator the total should calculate correctly.

    Try this code to see what the results are when you use Val/Dbl and comma/period, I've also added in an empty string to show what will happen if a textbox is empty.
    Msgbox Val("1,3")
    MsgBox Val("1.3")
    MsgBox CDbl("1,3")
    MsgBox CDbl("1.3")
    MsgBox Val("")
    MsgBox CDbl("")

  6. #6
    Forum Contributor MariaPap's Avatar
    Join Date
    10-19-2013
    Location
    Chania-Crete-Greece
    MS-Off Ver
    Excel 2003-2010
    Posts
    319

    Re: Wrong result adding values of text boxes in a userform.

    I get these results Mr Norie.

    First time i run your code i get 1

    click OK>>1,3
    click OK>>1,3
    click OK>>13
    click OK>>0
    and clicking OK again, i get an error with highlighting the last line of your code.

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Wrong result adding values of text boxes in a userform.

    Does this work?
    Private Sub prcase1_Change()
        SumTextBoxes
    End Sub
    
    Private Sub prcase2_Change()
        SumTextBoxes
    End Sub
    
    Private Sub prcase3_Change()
        SumTextBoxes
    End Sub
    
    Private Sub prcase4_Change()
        SumTextBoxes
    End Sub
    
    Private Sub TextBox6_Change()
        TextBox7.Text = Val(Replace(TextBox5.Text, ",", ".")) + Val(Replace(TextBox6.Text, ",", "."))
    End Sub
    
    Sub SumTextBoxes()
    Dim x As Long, Tot As Double
    
        Me.TextBox5.Text = ""
        
        For x = 1 To 4
            Tot = Tot + Val(Replace(Me("prcase" & x).Text, ",", "."))
        Next x
        
        Me.TextBox5.Text = Tot
        
    End Sub

  8. #8
    Forum Contributor MariaPap's Avatar
    Join Date
    10-19-2013
    Location
    Chania-Crete-Greece
    MS-Off Ver
    Excel 2003-2010
    Posts
    319

    Re: Wrong result adding values of text boxes in a userform.

    Of course it's works!!!!

    Thank you very much!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Help with code for userform text boxes, combo boxes and excel
    By innerise in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-20-2014, 09:07 AM
  2. [SOLVED] Populate cell values into 6 named text boxes in a userform then edit if required
    By How How in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 03-05-2013, 05:51 AM
  3. Replies: 17
    Last Post: 10-14-2012, 11:15 AM
  4. [SOLVED] Need code to add values from 2 text boxes and place result in a label.
    By egavasrg in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-01-2012, 08:12 PM
  5. Adding text boxes in userform
    By jcarstens in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-10-2009, 02:23 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1