+ Reply to Thread
Results 1 to 4 of 4

userform textbox calculation

Hybrid View

  1. #1
    Registered User
    Join Date
    01-27-2008
    Posts
    30

    userform textbox calculation

    Hello

    I Have a Userform that has 4 textboxes on it.


    Control Value
    textbox1 2
    textbox2 +
    textbox3 3
    textbox4 Results

    so basicly textbox2 will give me the sum of textbox1 + textbox3 = textbox4.text = 5 wich is the results.

    usually the formula or vba i use is

    textbox4 = VAL(textbox1) + VAL(Textbox3)
    but i need something like
    textbox4 = Val(textbox1) And VAL(textbox2) + VAL(textbox3)

  2. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    This userform code assumes four text boxes,

    TextBox1 holds a numeral
    TextBox2 holds a symbol; + - * / ^
    TextBox3 holds a numeral

    TextBox4 will display the result of the calculation.

    Private Sub TextBox1_Change()
        Call updateTextBoxes
    End Sub
    
    Private Sub TextBox2_Change()
        Call updateTextBoxes
    End Sub
    
    Private Sub TextBox3_Change()
        Call updateTextBoxes
    End Sub
    
    Sub updateTextBoxes()
       With Me
          .TextBox4.Text = vbNullString
        
          Select Case Left(Trim(.TextBox2.Text), 1)
             Case Is = "+"
                .TextBox4.Text = CStr(Val(.TextBox1.Text) + Val(.TextBox3.Text))
             Case Is = "-"
                .TextBox4.Text = CStr(Val(.TextBox1.Text) - Val(.TextBox3.Text))
             Case Is = "*"
                .TextBox4.Text = CStr(Val(.TextBox1.Text) * Val(.TextBox3.Text))
             Case Is = "/"
                If Val(.TextBox3.Text) = 0 Then
                    .TextBox4.Text = "#DIV/0"
                Else
                    .TextBox4.Text = CStr(Val(.TextBox1.Text) / Val(.TextBox3.Text))
                End If
              Case Is = "^"
                 On Error Resume Next
                    .TextBox4.Text = CStr(Val(.TextBox1.Text) ^ Val(.TextBox3.Text))
                 On Error GoTo 0
          End Select
        
       End With
    End Sub
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Another option:
    Sub updateTextBoxes()
       With Me
          .TextBox4.Value = Evaluate(.TextBox1.Text & .TextBox2.Text & TextBox3.Text)
        End With
    End Sub

  4. #4
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    Nice!

    I need to remember Evaluate.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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