dcsimg
Results 1 to 2 of 2

Thread: VBA Calculator

  1. #1

    Thread Starter
    Registered User
    Join Date
    Apr 2020
    Posts
    1

    VBA Calculator

    Hi All,

    Firstly sorry if i have posted in a wrong forum. But i need some help regarding VBA calculator which i have created recently.
    The calculator which i have created is working fine for any two numbers including decimal values. I have created an UI which looks as below:

    Name:  Capture.JPG
Views: 56
Size:  44.9 KB

    It takes input from textbox1 and when i click operators it will move and will display in textbox2. If i click equals even after entering multiple numbers and operators it calculates only the last two numbers entered. I want an output as the calculator should consider all the entered numbers in the textbox2 to get calculated.

    Now if i enter a number and operator and then another number if i click enter it is calculating the last two numbers entered. I need if the values entered mutiple times with operators to be calculated.

    Ex: Now it is working for 2+3=5
    but i need 2+3-5*4=? as of now its not working.

    So please help me out to how to achieve this in VBA?

    My code:
    Code:
    Dim firstnum As Double
    Dim secondnum As Double
    Dim answer As Double
    Dim operation As Double
    
    
    Private Sub CommandButton1_Click()
    
    TextBox1.Text = ""
    TextBox2.Text = ""
    
    End Sub
    
    Private Sub CommandButton10_Click()
    
    
    If TextBox1.Text = "0" Then
    TextBox1.Text = "1"
    Else
    TextBox1.Text = TextBox1.Text & "1"
    End If
    
    End Sub
    
    Private Sub CommandButton11_Click()
    
    
    If TextBox1.Text = "0" Then
    TextBox1.Text = "2"
    Else
    TextBox1.Text = TextBox1.Text & "2"
    End If
    
    End Sub
    
    Private Sub CommandButton12_Click()
    
    If TextBox1.Text = "0" Then
    TextBox1.Text = "3"
    Else
    TextBox1.Text = TextBox1.Text & "3"
    End If
    
    End Sub
    
    Private Sub CommandButton13_Click()
    
    If Not TextBox1.Text = "" Then
        firstnum = TextBox1.Text
    End If
    TextBox2.Text = TextBox2.Text & TextBox1.Text & "-"
    TextBox1.Text = ""
    operation = "-"
    
    End Sub
    
    Private Sub CommandButton14_Click()
    
    
    If TextBox1.Text = "0" Then
    TextBox1.Text = "0"
    Else
    TextBox1.Text = TextBox1.Text & "0"
    End If
    
    End Sub
    
    Private Sub CommandButton15_Click()
    
    If InStr(TextBox1.Text, ".") = 0 Then
    TextBox1.Text = TextBox1.Text & "."
    End If
    
    End Sub
    
    Private Sub CommandButton16_Click()
    
        If Not TextBox1.Text = "" Or TextBox1.Text = "." Then
                secondnum = TextBox1.Text
                
                        
                
       If operation = "+" Then
       answer = firstnum + secondnum
       TextBox1.Text = answer
       operation = ""
       ElseIf operation = "-" Then
       answer = firstnum - secondnum
       TextBox1.Text = answer
       operation = ""
       ElseIf operation = "*" Then
       answer = firstnum * secondnum
       TextBox1.Text = answer
       operation = ""
       ElseIf operation = "/" Then
            If secondnum = "0" Then
            MsgBox "Can't divide by Zero"
            Exit Sub
            End If
            answer = firstnum / secondnum
            TextBox1.Text = answer
            
    
    
       
       End If
       End If
       
    End Sub
    
    
    Private Sub CommandButton17_Click()
    
    If Not TextBox1.Text = "" Then
        firstnum = TextBox1.Text
    End If
    TextBox2.Text = TextBox2.Text & TextBox1.Text & "+"
    TextBox1.Text = ""
    operation = "+"
    
    End Sub
    
    Private Sub CommandButton2_Click()
    
    If TextBox1.Text = "0" Then
    TextBox1.Text = "7"
    Else
    TextBox1.Text = TextBox1.Text & "7"
    End If
    
    End Sub
    
    Private Sub CommandButton3_Click()
    
    
    If TextBox1.Text = "0" Then
    TextBox1.Text = "8"
    Else
    TextBox1.Text = TextBox1.Text & "8"
    End If
    
    End Sub
    
    Private Sub CommandButton4_Click()
    
    
    If TextBox1.Text = "0" Then
    TextBox1.Text = "9"
    Else
    TextBox1.Text = TextBox1.Text & "9"
    End If
    
    End Sub
    
    Private Sub CommandButton5_Click()
    
    If Not TextBox1.Text = "" Then
        firstnum = TextBox1.Text
    End If
    TextBox2.Text = TextBox2.Text & TextBox1.Text & "/"
    TextBox1.Text = ""
    operation = "/"
    
    End Sub
    
    Private Sub CommandButton6_Click()
    
    
    If TextBox1.Text = "0" Then
    TextBox1.Text = "4"
    Else
    TextBox1.Text = TextBox1.Text & "4"
    End If
    
    End Sub
    
    Private Sub CommandButton7_Click()
    
    
    If TextBox1.Text = "0" Then
    TextBox1.Text = "5"
    Else
    TextBox1.Text = TextBox1.Text & "5"
    End If
    
    End Sub
    
    Private Sub CommandButton8_Click()
    
    
    If TextBox1.Text = "0" Then
    TextBox1.Text = "6"
    Else
    TextBox1.Text = TextBox1.Text & "6"
    End If
    
    End Sub
    
    Private Sub CommandButton9_Click()
    
    If Not TextBox1.Text = "" Then
        firstnum = TextBox1.Text
    End If
    TextBox2.Text = TextBox2.Text & TextBox1.Text & "*"
    TextBox1.Text = ""
    operation = "*"
    
    End Sub
    Last edited by Shaggy Hiker; Apr 29th, 2020 at 09:35 AM. Reason: Split out, and fixed up.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,796

    Re: Starting A New Thread

    Oh dear gawd... you did all sorts of things wrong... 1) you posted in the wrong spot - this is Forum Feedback, 2) you just hijacked a thread, 3) you didn't even bother to read what the thread was about ... post #2 at the top even tells you how to post your own new thread - which is what THIS thread was all about, 4) you posted unformatted code - next time, after pasting the code, select it, and clock the "#" button in the toolbar above...

    I'll ask the mods to split your post off and move it to a more appropriate spot.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width