• 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.
  • Posts

    • It's just exactly what it looks like. The facial polygon count is low and rigid, and the background appears heavily blurred. You can clearly see jagged edges around the car window as it exits the tunnel. The character animations feel like they're from 2010. The Xbox Series S has performance similar to an RX 570 or GTX 1660, which is disappointing and contributes to holding back game development. The Switch GPU is roughly on par with a 2050 or 3050, which is even worse. Developers have often discussed the challenges and limitations of developing for outdated/low end hardware, noting how it can hold back progress. This isn't a new issue. I am sure we will hear about the "Challenges" they had to overcome here and sacrifices that had to be made for compatibility with the outdated/low end hardware.
    • Whether on Edge or Firefox, I don't get this prompt to accept cookies, because they're already whitelisted by the Cookie AutoDelete extension, both on Edge and Firefox. And, as I've already said, using Edge (yesterday), Neowin isn't whitelisted (I never use Edge), and I didn't get that pop-up. And this morning, after checking, still no pop-up, and everything is activated, the proof (screenshot), I have no ads on the right side. I must add that I block the following trackers Doubleclick, and Scorecardresearch, as well as a few other undesirable ones, via the Host file. Back to square one! After all, it's a simple click to continue, so I'll do with it. 🤷🏽‍♂️ Thank You
    • F'ing FIIINALLY! Much easier to ask people what their hardware is 🙏 and could also very well just say DDR4 or DDR5 without asking to bloat it too much. I'm also curious to see what it will say in More device info...
    • Sure, that must be why it was revealed at the Sony showcase... the game was announced 5 years ago and the visuals are far from poor, what the hell are you smoking, plus the Switch 2 is pretty much on par with an Xbox Series S. It would be absolutely insane for a dev to build a game "with the Switch 2 in mind" when it's coming to all platforms where the majority of sales will happen.
  • Recent Achievements

    • Week One Done
      jbatch earned a badge
      Week One Done
    • First Post
      Yianis earned a badge
      First Post
    • Rookie
      GTRoberts went up a rank
      Rookie
    • First Post
      James courage Tabla earned a badge
      First Post
    • Reacting Well
      James courage Tabla earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      400
    2. 2
      +FloatingFatMan
      178
    3. 3
      snowy owl
      170
    4. 4
      ATLien_0
      167
    5. 5
      Xenon
      134
  • Tell a friend

    Love Neowin? Tell a friend!