Microsoft Excel Webinar

Results 1 to 6 of 6

Thread: Sum text box values

  1. #1

    Sum text box values

    This seems so simple I'm almost embarressed to ask but:
    I have a form where I want a text box to display the total of the textbox values on the form.

    VB:
    Private Sub Calculate() 
        On Error Resume Next 
        TextBox3.Value = TextBox1.Value + TextBox2.Value 
         ' TextBox3.Value = (Format(TextBox, "0.00"))
    End Sub 
    Private Sub TextBox1_Change() 
        OnlyNumbers 
        Calculate 
    End Sub 
    Private Sub TextBox2_Change() 
        OnlyNumbers 
        Calculate 
    End Sub 
     
    Private Sub OnlyNumbers() 
        If TypeName(Me.ActiveControl) = "TextBox" Then 
            With Me.ActiveControl 
                If Not IsNumeric(.Value) And .Value <> vbNullString Then 
                    MsgBox "Sorry, only numbers allowed" 
                    .Value = vbNullString 
                End If 
            End With 
        End If 
         
    End Sub 
    
    
    Formatting tags added by mark007
    This works when multiplying the values
    VB:
    TextBox3.Value = TextBox1.Value * TextBox2.Value 
    
    
    Formatting tags added by mark007
    So i must be in the right area???

    As an add on I would like the total to be rounded to be the sum of textbox1 and textbox2, divided by 1000 and dsiplayed to 2 figures

    eg

    1234 + 3456 = 4.69

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master xld's Avatar
    Joined
    Apr 2005
    Posts
    23,939
    Location
    VB:
     
    TextBox3.Value = Format((CDbll(TextBox1.Value) + CDbl(TextBox2.Value)) / 1000, "#,##0.00") 
    
    
    Formatting tags added by mark007

  3. #3

    sub or function not defined

    XLD
    If I just copy your code:
    VB:
    TextBox3.Value = Format((CDbll(TextBox1.Value) + [COLOR=blue]CDbl[/COLOR](TextBox2.Value)) / 1000, "#,##0.00") 
    
    
    Formatting tags added by mark007
    over my line
    VB:
    TextBox3.Value = TextBox1.Value + TextBox2.Value 
    
    
    Formatting tags added by mark007
    I get an error:

    "Sub or function not defined."

  4. #4
    Look at the first CDbl function call (two "l"s). It's a typo.
    Glen

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master xld's Avatar
    Joined
    Apr 2005
    Posts
    23,939
    Location
    Quote Originally Posted by lifeson
    XLD
    If I just copy your code:
    VB:
    TextBox3.Value = Format((CDbll(TextBox1.Value) + [COLOR=blue]CDbl[/COLOR](TextBox2.Value)) / 1000, "#,##0.00") 
    
    
    Formatting tags added by mark007
    over my line
    VB:
    TextBox3.Value = TextBox1.Value + TextBox2.Value 
    
    
    Formatting tags added by mark007
    I get an error:

    "Sub or function not defined."
    Sorry, I changed the Val to CDbl just before posting (Because of an issue with another post a few days ago), but messed it. It should be

    VB:
    TextBox3.Value = Format((CDbl(TextBox1.Value) + [COLOR=blue]CDbl[/COLOR](TextBox2.Value)) / 1000, "#,##0.00") 
    
    
    Formatting tags added by mark007

  6. #6
    Cheers
    As usual thanx for the help

Posting Permissions

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