• 0

[VBA] Advanced Calculator / Optimisation


Question

me and this other student in my class got bored... so we have started making a calculator

this has soon become a battle to who makes the best one.. ( no scientific / graphical functions allowed)

I have done the most functions so far but i cannot get my head around a simple backspace button

I have my current form and Code attached i just need a help with this backspace button and also any other ideas you can think of for it..

Private Sub btn1_Click()
	If LblDisplay = "0" Then
		LblDisplay = "1"
	Else
		LblDisplay = LblDisplay + "1"
	End If
End Sub

Private Sub btn2_Click()
	If LblDisplay = "0" Then
		LblDisplay = "2"
	Else
		LblDisplay = LblDisplay + "2"
	End If
 End Sub

Private Sub btn3_Click()
	If LblDisplay = "0" Then
		LblDisplay = "3"
	Else
		LblDisplay = LblDisplay + "3"
	End If
 End Sub

Private Sub btn4_Click()
	If LblDisplay = "0" Then
		LblDisplay = "4"
	Else
		LblDisplay = LblDisplay + "4"
	End If
 End Sub

Private Sub btn5_Click()
	 If LblDisplay = "0" Then
		LblDisplay = "5"
	Else
		LblDisplay = LblDisplay + "5"
	 End If
 End Sub

Private Sub btn6_Click()
	If LblDisplay = "0" Then
		LblDisplay = "6"
	Else
		LblDisplay = LblDisplay + "6"
	End If
 End Sub

Private Sub btn7_Click()
	 If LblDisplay = "0" Then
		LblDisplay = "7"
	Else
		LblDisplay = LblDisplay + "7"
	End If
 End Sub

Private Sub btn8_Click()
	If LblDisplay = "0" Then
		LblDisplay = "8"
	Else
		LblDisplay = LblDisplay + "8"
	End If
 End Sub

Private Sub btn9_Click()
	If LblDisplay = "0" Then
		LblDisplay = "9"
	Else
		LblDisplay = LblDisplay + "9"
	End If
 End Sub

Private Sub btn0_Click()
	If LblDisplay = "0" Then
		LblDisplay = "0"
	Else
		LblDisplay = LblDisplay + "0"
	End If
 End Sub

Private Sub BtnClear_Click()
	Num1 = 0
	num2 = 0
	LblDisplay = 0
End Sub

Private Sub BtnAdd_Click()
	Num1 = LblDisplay
	LblDisplay = "0"
	Ftn = "Add"
End Sub

Private Sub BtnDec_Click()
	 If InStr(LblDisplay.Caption, ".") = 0 Then
		  LblDisplay.Caption = LblDisplay.Caption & "."
	 End If
End Sub

Private Sub BtnDiv_Click()
	Num1 = LblDisplay
	LblDisplay = "0"
	Ftn = "Div"
End Sub

Private Sub BtnExit_Click()
	Unload FrmAdvCalc
End Sub

Private Sub BtnMul_Click()
	Num1 = LblDisplay
	LblDisplay = "0"
	Ftn = "Mul"
End Sub

Private Sub BtnRec_click()

End Sub

Private Sub BtnSub_Click()
	Num1 = LblDisplay
	LblDisplay = "0"
	Ftn = "Subt"
End Sub

Private Sub BtnInv_click()
	LblDisplay = LblDisplay * "-1"
End Sub

Private Sub BtnRecip_click()
	LblDisplay = "1" / LblDisplay
End Sub

Private Sub btnClrScr_Click()
LblDisplay = "0"
End Sub

Private Sub BtnEql_Click()
	If Ftn = "Subt" Then
		num2 = LblDisplay
		LblDisplay = Num1 - num2
		Num1 = LblDisplay
	ElseIf Ftn = "Add" Then
		num2 = LblDisplay
		LblDisplay = Num1 + num2
		Num1 = LblDisplay
	ElseIf Ftn = "Mul" Then
		num2 = LblDisplay
		LblDisplay = Num1 * num2
		Num1 = LblDisplay
	ElseIf Ftn = "Div" And LblDisplay = "0" Then
		LblDisplay = "Cannot Divide By 0"
		Else
		num2 = LblDisplay
		LblDisplay = Num1 / num2
		Num1 = LblDisplay
	End If
End Sub

Thankyou in advance

post-162082-1191064770.jpg

Link to comment
https://www.neowin.net/forum/topic/590875-vba-advanced-calculator-optimisation/
Share on other sites

6 answers to this question

Recommended Posts

  • 0

Learn about converting infix expressions into postfix expressions, so you can use a simple system to calculate expressions like (3+4)*(7-(4+8)/2) that get pasted in.

Here's how to covert it:

http://scriptasylum.com/tutorials/infix_po...stfix/index.htm

Here's how to evaluate the resultant postfix expression:

http://scriptasylum.com/tutorials/infix_po...ation/index.htm

(Converting infix to postfix and evaluating was actually one of our first assignments back when I took my intro to computer science class.)

Also, much more importantly, stop using VBA.

Learn something more useful like VB.NET or C#.

http://msdn2.microsoft.com/en-us/express/default.aspx

  • 0

Hehe.. thanks for that... i never actually learnt that...

i do other languages aswell ( inc. Java , haskell , C , pascal) but VBA is all we have access to at college...

thanks for the links...

UPDATE : Now Inlcuded a BkSpace Button..

Now In Need Of %age Function ... 1st post to be updated soon

  • 0
  MioTheGreat said:
Also, much more importantly, stop using VBA... Learn something more useful like VB.NET or C#.

VBA is very useful - it's how a lot of people earn their living...

Yantrio - You should get into the habit of declaring all of your variables (Num1, Num2, Ftn).

In the IDE, go to Tools, Options and on the Editor tab, check "Require Variable Declaration". This will add Option Explicit to all new modules. It makes debugging a lot easier and also helps to prevent mistyping of variable names.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.