Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,256
Total Threads: 106,569
Total Posts: 592,689
There are 88 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     .NET and MSSQL :

My VB Homework

Thread title: My VB Homework
Reply    
    Thread tools Search this thread Display Modes  
02-08-2007, 11:13 PM
#1
Jack Morrison is offline Jack Morrison
Jack Morrison's Avatar
Status: Member
Join date: Dec 2005
Location: Ontario, Canada
Expertise:
Software:
 
Posts: 264
iTrader: 0 / 0%
 

Jack Morrison is on a distinguished road

Send a message via AIM to Jack Morrison Send a message via MSN to Jack Morrison Send a message via Yahoo to Jack Morrison Send a message via Skype™ to Jack Morrison

  Old  My VB Homework

VB is new to me, dont laugh.

Part of the assignment is to make a simple calculator macro in Microsoft Excel. I've tested it and it seems to work pretty well, just wanted to post the code and see if anyone sees any major errors or improvements I can make.

Code:
Option Explicit
'Written by Jack Morrison

Sub mytoycalculator()
Dim Num1 As Integer
Dim Num2 As Integer
Dim Operator As String * 1
Dim Answer As Integer
Dim Success As Boolean

Num1 = InputBox("Enter the First Value")
Num2 = InputBox("Enter the Second Value")
Operator = InputBox("Enter the sign of the operation")

If Operator = "+" Then
Answer = (Num1 + Num2)
Success = True
ElseIf Operator = "-" Then
Answer = (Num1 - Num2)
Success = True
ElseIf Operator = "*" Then
Answer = (Num1 * Num2)
Success = True
ElseIf Operator = "^" Then
Answer = (Num1 ^ Num2)
Success = True
ElseIf Operator = "/" And (Num2 <> 0) Then
Answer = (Num1 / Num2)
Success = True
Else
Success = False
End If

If Success = True Then
MsgBox ("The Operation was successful, The Answer is " & Answer)
 
ElseIf Success = False Then
MsgBox ("The Operation Failed")
End If
End Sub
Thanks,

Axiom

Reply With Quote
02-08-2007, 11:46 PM
#2
Shane is offline Shane
Status: Member
Join date: Aug 2005
Location:
Expertise:
Software:
 
Posts: 346
iTrader: 0 / 0%
 

Shane is on a distinguished road

  Old

I don't see anything majorly wrong with it (I haven't messed with vb in over a year!) but is their a module along with this hence the Option Explicit? I can't remember if that has to do with a module or if you can use it with anything.

Reply With Quote
02-08-2007, 11:56 PM
#3
Salathe is offline Salathe
Salathe's Avatar
Status: Community Archaeologist
Join date: Jul 2004
Location: Scotland
Expertise: Software Development
Software: vim, PHP
 
Posts: 3,820
iTrader: 25 / 100%
 

Salathe will become famous soon enough

Send a message via MSN to Salathe

  Old

Personally, I'd indent the code to make things more clear but that's a personal preference. Also, I'd have gone with a "Select Case" statement rather than lots of "if ... then ... elseif" statements -- you can always catch the division-by-zero separately.

Reply With Quote
02-09-2007, 02:44 AM
#4
Shane is offline Shane
Status: Member
Join date: Aug 2005
Location:
Expertise:
Software:
 
Posts: 346
iTrader: 0 / 0%
 

Shane is on a distinguished road

  Old

I forgot all about select case. I got a good ol' vb book here and I would be glad to scan the "Select Case" pages if you would like. Also as Salathe said, indent your code and maybe even put comments by the appostraphe to remember what each thing is for.

Reply With Quote
02-09-2007, 04:10 PM
#5
Jack Morrison is offline Jack Morrison
Jack Morrison's Avatar
Status: Member
Join date: Dec 2005
Location: Ontario, Canada
Expertise:
Software:
 
Posts: 264
iTrader: 0 / 0%
 

Jack Morrison is on a distinguished road

Send a message via AIM to Jack Morrison Send a message via MSN to Jack Morrison Send a message via Yahoo to Jack Morrison Send a message via Skype™ to Jack Morrison

  Old

No idea what the "select case" statement is, haven't covered that yet I guess.

Reply With Quote
02-09-2007, 06:23 PM
#6
Salathe is offline Salathe
Salathe's Avatar
Status: Community Archaeologist
Join date: Jul 2004
Location: Scotland
Expertise: Software Development
Software: vim, PHP
 
Posts: 3,820
iTrader: 25 / 100%
 

Salathe will become famous soon enough

Send a message via MSN to Salathe

  Old

Axiom: If you've not yet covered Select Case, then head on over here.

Reply With Quote
07-15-2009, 05:28 PM
#7
neodream is offline neodream
Status: I'm new around here
Join date: Jul 2009
Location: aberdeen
Expertise:
Software:
 
Posts: 8
iTrader: 0 / 0%
 

neodream is on a distinguished road

Send a message via Skype™ to neodream

  Old

hi

Reply With Quote
Reply    


Thread Tools
Display Modes

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed