Urban75 Home About Offline BrixtonBuzz Contact

How d'you create simple calculator in Excel?

Dim A as integer
Dim B as double
Dim C as double
Dim D as double

You've not diplayed the value D at the end have you?

Stick this line in just before you clear calcstring
Sheet1.Range("A1").Value = D

Where A1 is the cell you want to use as the calculators display. :)

Then you might want to add extra code to each of the command_click routines so the calculator displays each key press so you know what you have entered.
:)
 
Thanks - here we go:

Option Explicit

Private Sub Button_dec_point_Click()
'adds dec point to string

Calcstring = Calcstring + "."

End Sub

Private Sub Button_divide_Click()
'adds / to string

Calcstring = Calcstring + "/"
End Sub

Private Sub Button_equals_Click()
Dim A As Integer
Dim B As Double
Dim C As Double
Dim D As Double


A = InStr(Calcstring, "+")
If A > 0 Then
B = Val(Left(Calcstring, A))
C = Val(Mid(Calcstring, A + 1))
D = A + B
End If

A = InStr(Calcstring, "-")
If A > 0 Then
B = Val(Left(Calcstring, A))
C = Val(Mid(Calcstring, A + 1))
D = A - B
End If

A = InStr(Calcstring, "*")
If A > 0 Then
B = Val(Left(Calcstring, A))
C = Val(Mid(Calcstring, A + 1))
D = A * B
End If

A = InStr(Calcstring, "/")
If A > 0 Then
B = Val(Left(Calcstring, A))
C = Val(Mid(Calcstring, A + 1))
D = A / B
End If

Sheets("Calculator").Range("C4").Value = D



End Sub

Private Sub Button_minus_Click()
'adds - to string

Calcstring = Calcstring + "-"
End Sub

Private Sub Button_multiply_Click()
'adds * to string
Calcstring = Calcstring + "*"
End Sub

Private Sub Button_plus_Click()
'adds + to string

Calcstring = Calcstring + "+"

End Sub

Private Sub Button0_Click()
'adds 0 to string

Calcstring = Calcstring + "0"

End Sub

Private Sub Button1_Click()
'adds 1 to string

Calcstring = Calcstring + "1"
End Sub

Private Sub Button2_Click()
'adds 2 to string

Calcstring = Calcstring + "2"
End Sub

Private Sub Button3_Click()
'adds 3 to string

Calcstring = Calcstring + "3"
End Sub

Private Sub Button4_Click()
'adds 4 to string

Calcstring = Calcstring + "4"
End Sub

Private Sub Button5_Click()
'adds 5 to string

Calcstring = Calcstring + "5"
End Sub

Private Sub Button6_Click()
'adds 6 to string

Calcstring = Calcstring + "6"
End Sub

Private Sub Button7_Click()
'adds 7 to string

Calcstring = Calcstring + "7"
End Sub

Private Sub Button8_Click()
'adds 8 to string

Calcstring = Calcstring + "8"
End Sub

Private Sub Button9_Click()
' adds 9 to string

Calcstring = Calcstring + "9"
End Sub

Private Sub CommandButton1_Click()
'clears
Sheets("Calculator").Range("C4").Value = "0"
End Sub
 
After the sheets.........value=D you need to put

CalcString=""

to clear it ready for the next calculation. Your clear routine also needs that statement in or you just clear the 'display'.

The Dim statement for the CalcString should be in 'general' 'declarations' section.
 
Thanks, WouldBe.

Doing the command buttons was a piece of p. It's trying to do the VBA when we ain't been taught it that's the problem.

I'm just off to try what you suggested ...

Watch this space ...
 
tbh - it's still not working. But at least now the ans isn't 4 every time. It seems to think that 7*7=14, and worse.

I have created a Clear command button, and set A,B, C and D to go to zero as well when it is pressed.
 
You shouldn't need to clear A,B,C,D as they are set explicitly in the sub routines.

Right click on the * key and then macro and just check that your not adding a + to calcstring instead of a *.
 
Got it! The code you did for me WouldBe, you're doing the op on the A and B, it should be the B and C.

It seems to be working ok now.
 
electric.avenue said:
Got it! The code you did for me WouldBe, you're doing the op on the A and B, it should be the B and C.

It seems to be working ok now.
Dozy twat @self :rolleyes:

That's my brain on the blink again. That's why it took me 10 minutes to remember how to stick buttons on a sheet. Yet when I put the code behind the buttons I put it in right.
 
Seems strange they asked you to do this without teaching you VBA.:confused:

I've just tried nested IF functions in the spread sheet and they work (which I didn't think they would)

=IF(B10="+",A10+C10,IF(B10="-",A10-C10,IF(B10="*",A10*C10,IF(B10="/",A10/C10))))

Sticking that formula into D10 with the first number in A10, the operator in B10 and the second number in C10 works. :eek:

It said teh formula was invalid but still runs it.
 
Back
Top Bottom