6

Click here to load reader

Visual BasicEx1

Embed Size (px)

Citation preview

Page 1: Visual BasicEx1

8/9/2019 Visual BasicEx1

http://slidepdf.com/reader/full/visual-basicex1 1/6

Visual Basic for Application

Ex1. The Arithmetic Program1. Open MS Excel2. Select Developer tab→ Visual basic See !ig 1.". Shortcut is b# pressing Alt $

!11%. !rom the menu& select 'nsert& then choose (ser!orm. A )orm *ith the name

(ser!orm1 an+ a toolbox containing controls *ill be +ispla#e+,. -lic Vie* an+ choose pro/ect explorer an+ properties *in+o* i) the# are not

+ispla#e+&0. To a++ controls& /ust select the +esire+ control an+ insert it an#*here in the

)orm. A++ the )ollo*ing controls comman+ "& textbox 0"& labels 0" . See!ig. 2

3. Set the properties o) the controls accor+ing to Table 1.

 Table 1 The Properties o) The Arithmetic Program Object Property Value(ser!orm1 4ame

-aption)rmArithmetic

 The Arithmetic Program Text box1 4ame txt4um1

 Text box2 4ame txt4um2 Text box% 4ame txtAns*er Text box, 4ame txtString1 Text box0 4ame txtString25abel1 4ame

-aptionlbl'n)o

 This program +emonstrates arithmeticoperations

5abel2 4ame-aption

lbl'nt1Enter the 6rst o) t*o numbers

5abel% 4ame-aption

lbl'nt2Enter the secon+ o) t*o numbers

5abel, 4ame-aption

lblString1 T#pe 6rst string

5abel0 4ame-aption

lblString2 T#pe secon+ string

-omman+1 4ame-aption

cm+A++A++

-omman+2 4ame-aption

cm+SubtractSubtract

-omman+% 4ame-aption

cm+Multipl#Multipl#

-omman+, 4ame-aption

cm+Divi+eDivi+e

-omman+0 4ame-aption

cm+-ombine-oncatenate

-omman+3 4ame-aption

cm+-lear-lear

Page 2: Visual BasicEx1

8/9/2019 Visual BasicEx1

http://slidepdf.com/reader/full/visual-basicex1 2/6

-omman+7 4ame-aption

cm+ExitExit

-omman+ 4ame-aption

cm+-onString-oncatenate String

!ig. 1

Page 3: Visual BasicEx1

8/9/2019 Visual BasicEx1

http://slidepdf.com/reader/full/visual-basicex1 3/6

!ig. 2

8riting the co+es

4ote 9ou can /ust cop# the co+es )rom this page then paste it in the Visual basicpage.Each time a)ter #ou have *ritten the co+e& run it b# pressing !0 or choose :un)rom the menu.

 To s*itch bet*een -o+e vie* an+ ob/ect vie* & clic these icons locate+ /ustbelo* the Pro/ect explorer.

1. The simplest co+e is )or exit. Double clic the Exit button an+ #ou *ill seeb# +e)ault"

Private Sub cm+Exit;-lic"

En+ Sub

2. T#pe En+ bet*een the t*o lines so that it becomesPrivate Sub cm+Exit;-lic"En+En+ Sub

%. To see the e<ect o) this co+e #ou run the program b# pressing !0 or choose:un in the menu then choose :un Sub=(ser)orm. -lic the exit button an+ it*ill close the )orm

Page 4: Visual BasicEx1

8/9/2019 Visual BasicEx1

http://slidepdf.com/reader/full/visual-basicex1 4/6

,. 4o* +ouble>clic the A++ button. Again #ou *ill see the t*o linesPrivate Sub cm+A++;-lic"

En+ Sub

0. A++ the )ollo*ing co+es )or A++ button"so that *hen complete it loos liePrivate Sub cm+A++;-lic"Dim 4um1 As SingleDim 4um2 As SingleDim Ans As Single

4um1 ? txt4um14um2 ? txt4um2Ans ? 4um1 $ 4um2txtAns*er ? Ans

En+ Sub3. :epeat the same )or the other buttons i.e Multipl#& Divi+e an+ -oncatenate.

 The )ollo*ing are the co+es )or each button. :emember 6rst +ouble clic thatbutton i.e Multipl#& Divi+e an+ -oncatenate" then insert the co+es bet*eenthe t*o lines Private Sub an+ En+ Sub.

7. 9ou can actuall# cop# an+ paste this co+e. -op#)rom this page an+ thenpaste it beteen the t*o lines.

-o+es )or Subtract the operator is >"

Private Sub cm+Subtract;-lic"Dim 4um1 As SingleDim 4um2 As SingleDim Ans As Single

4um1 ? txt4um14um2 ? txt4um2Ans ? 4um1 > 4um2txtAns*er ? Ans

En+ Sub

  -o+es )or Multipl#the operator is @"

Private Sub cm+Multipl#;-lic"Dim 4um1 As SingleDim 4um2 As SingleDim Ans As Single

4um1 ? txt4um14um2 ? txt4um2Ans ? 4um1 @ 4um2

Page 5: Visual BasicEx1

8/9/2019 Visual BasicEx1

http://slidepdf.com/reader/full/visual-basicex1 5/6

txtAns*er ? Ans

En+ Sub

-o+es )or Divi+e the operator is =". Private Sub cm+Divi+e;-lic"

Dim 4um1 As SingleDim 4um2 As SingleDim Ans As Single

4um1 ? txt4um14um2 ? txt4um2Ans ? 4um1 = 4um2txtAns*er ? Ans

En+ Sub

-o+es )or -oncatenate the operator is "

 The -oncatenate operator /oins the t*o numbers togetherPrivate Sub cm+-ombine;-lic"

Dim 4um1 As SingleDim 4um2 As SingleDim Ans As Single

4um1 ? txt4um14um2 ? txt4um2Ans ? 4um1 4um2txtAns*er ? Ans

En+ Sub

B. 8e nee+ to clear the entries numbers an+ ans*ers" i) *e *ant to repeat thecalculations using +i<erent numbers. 8e have create+ the -lear button orcontrol. The co+e to clear is as )ollo*s

-o+es )or -lear

Private Sub cm+-lear;-lic"txt4um1 ? CCtxt4um2 ? CCtxtAns*er ? CC

En+ Sub

1.8e *ill use the concatenate operator on string +atat#pe ie. text or numbers$ text.

-o+es )or -oncatenate stringsPrivate Sub cm+-onString;-lic"txtAns*er ? txtString1 txtString2

Page 6: Visual BasicEx1

8/9/2019 Visual BasicEx1

http://slidepdf.com/reader/full/visual-basicex1 6/6

En+ Sub11.To clear the input )or the strings& a++ t*o more lines to the -lear button. The

complete co+e )or the -lear button loos lie this

Private Sub cm+-lear;-lic"

txt4um1 ? CCtxt4um2 ? CCtxtAns*er ? CCtxtString1 ? CCtxtString2 ? CC

En+ Sub12.!inal +ispla# *hen #ou run the program is as sho*n in !ig. %. 4ote the

concatenate string button *as a++e+ later so it *as not seen in !ig. 2"

!ig.%