VBA in Excel Calculator-VBForums
Results 1 to 6 of 6

Thread: VBA in Excel Calculator

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2011
    Posts
    4

    VBA in Excel Calculator

    Hi all

    I have created a simple calculator in Excel - please excuse the awful layout and coding - I am fairly new

    Please can you point me the in the right direction of making it so the calculator can take more than two inputs and calculate them?

    So at the moment it can do x+y etc. and I want it to be able to do x+y+z and more.

    Thanks!
    vb Code:
    1. Private FirstNumber As Long  ' Store user-defined numbers
    2. Private SecondNumber As Long
    3. Private Operation As Integer    ' Store operation type
    4.  
    5. Private Sub button1_Click()
    6.    Me.TextBox1.Value = Me.TextBox1.Value & 1
    7. End Sub
    8.  
    9. Private Sub button0_Click()
    10. If Me.TextBox1.Value = 0 Then Me.TextBox1.Value = 0 _
    11. Else Me.TextBox1.Value = Me.TextBox1.Value & 0
    12.  
    13. End Sub
    14.  
    15. Private Sub button9_Click()
    16. If Me.TextBox1.Value = 0 Then Me.TextBox1.Value = 9 _
    17. Else Me.TextBox1.Value = Me.TextBox1.Value & 9
    18. End Sub
    19.  
    20. Private Sub button2_Click()
    21. If Me.TextBox1.Value = 0 Then Me.TextBox1.Value = 2 _
    22. Else Me.TextBox1.Value = Me.TextBox1.Value & 2
    23. End Sub
    24.  
    25. Private Sub button7_Click()
    26. If Me.TextBox1.Value = 0 Then Me.TextBox1.Value = 7 _
    27. Else Me.TextBox1.Value = Me.TextBox1.Value & 7
    28. End Sub
    29.  
    30. Private Sub button8_Click()
    31. If Me.TextBox1.Value = 0 Then Me.TextBox1.Value = 8 _
    32. Else Me.TextBox1.Value = Me.TextBox1.Value & 8
    33. End Sub
    34.  
    35. Private Sub button5_Click()
    36. If Me.TextBox1.Value = 0 Then Me.TextBox1.Value = 5 _
    37. Else Me.TextBox1.Value = Me.TextBox1.Value & 5
    38. End Sub
    39.  
    40. Private Sub button6_Click()
    41. If Me.TextBox1.Value = 0 Then Me.TextBox1.Value = 6 _
    42. Else Me.TextBox1.Value = Me.TextBox1.Value & 6
    43. End Sub
    44.  
    45. Private Sub button3_Click()
    46. If Me.TextBox1.Value = 0 Then Me.TextBox1.Value = 3 _
    47. Else Me.TextBox1.Value = Me.TextBox1.Value & 3
    48. End Sub
    49.  
    50. Private Sub button4_Click()
    51. If Me.TextBox1.Value = 0 Then Me.TextBox1.Value = 4 _
    52. Else Me.TextBox1.Value = Me.TextBox1.Value & 4
    53. End Sub
    54.  
    55. Private Sub CommandAdd_Click()
    56.  
    57. FirstNumber = Me.TextBox1.Text        ' Store first number
    58.  
    59. Me.TextBox1.Text = ""
    60.  
    61. Operation = 0                  ' Select operation type
    62.  
    63. End Sub
    64.  
    65. Private Sub CommandSubtract_Click()
    66. FirstNumber = Me.TextBox1.Text        ' Store first number
    67. Me.TextBox1.Text = ""
    68. Operation = 1                  ' Select operation type
    69. End Sub
    70.  
    71. Private Sub CommandDivide_Click()
    72. FirstNumber = Me.TextBox1.Text        ' Store first number
    73.  
    74. Me.TextBox1.Text = ""
    75.  
    76. Operation = 2
    77. End Sub
    78.  
    79. Private Sub CommandMultiply_Click()
    80. FirstNumber = Me.TextBox1.Text        ' Store first number
    81.  
    82. Me.TextBox1.Text = ""
    83.  
    84. Operation = 3
    85. End Sub
    86.  
    87. Private Sub CommandEquals_Click()
    88. SecondNumber = Me.TextBox1.Text      ' Store second number
    89. Select Case Operation          ' Look to see what operation user selected
    90.  
    91.   Case 0 ' Addition
    92.  
    93.     Me.TextBox1.Text = FirstNumber + SecondNumber ' Perform operation
    94.  
    95.   Case 1 ' Subtraction
    96.  
    97.     Me.TextBox1.Text = FirstNumber - SecondNumber ' Perform operation
    98.    
    99.      Case 2 ' Division
    100.  
    101.     Me.TextBox1.Text = FirstNumber / SecondNumber ' Perform operation
    102.    
    103.      Case 3 ' Multiplication
    104.  
    105.     Me.TextBox1.Text = FirstNumber * SecondNumber ' Perform operation
    106.  
    107. End Select
    108.  
    109. End Sub
    110.  
    111. Private Sub Clear_Click() 'clears calculator
    112. Me.TextBox1.Value = 0
    113. FirstNumber = 0
    114. SecondNumber = 0
    115. End Sub
    Last edited by Hack; Dec 1st, 2011 at 06:27 AM. Reason: Added Highlight Tags

  2. #2
    Super Moderator Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,334

    Re: VBA in Excel Calculator

    Welcome to the forums

    I'm a little curious as to why you are concatenating the numbers rather than adding them.
    vb Code:
    1. 'if the number 5 was in the textbox then this
    2. Me.TextBox1.Value = Me.TextBox1.Value & 8
    3. 'would result in 58 being in the textbox as opposed to
    4. Me.TextBox1.Value = Me.TextBox1.Value + 8
    5. 'would result in 13 being in the textbox
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2011
    Posts
    4

    Re: VBA in Excel Calculator

    Hi thanks for the reply,

    I'm concatenating the numbers so that when the number 1,2,3,4,5,6,7,8,9,0 button is pressed then it appears i the text box. It then stays there and if another number button is pressed the current data shown in the text box is not replaced by the new button press but added to the end. This allowing two, three, four digit numbers to be displayed. Hope I explained it ok?

    Jasmin

  4. #4
    Super Moderator Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,334

    Re: VBA in Excel Calculator

    Ok...they are being displayed, but they are not being added together. Isn't that what you want your calculator to do?
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2011
    Posts
    4

    Re: VBA in Excel Calculator

    yeah that's what I have the CommandSubtract, CommandAdd, CommandMultiply, ComandDivide buttons doing. When they are clicked the input in the textbox is stored in FirstNumber and then the text box is cleared to make way for a second input and an operation number is assigned to the value. The CommandEquals button stores the second input as SecondNumber and performs add, multiply,divide or subtract (according to the operation number) on the two numbers.

    So basically it works perfectly for x+y (two inputs) but not for x+y+z+a etc... (multiple inputs)

  6. #6
    Addicted Member
    Join Date
    Jun 2009
    Location
    Townsville, Qld, Australia
    Posts
    135

    Re: VBA in Excel Calculator

    I haven't programmed a calculator but I have written programs that add and multiply busily. To do that, instead of having "firstnumber" and "secondnumber" and so on, I used arrays.

    Once you get into 3 or more numbers, you are also going to have to look at the order of operations, e.g. (14/7) + 3 <> 14/(7 + 3). You may also want to allow the user to insert brackets to allow for more complex operations.

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

Survey posted by VBForums.