Download presentation
Presentation is loading. Please wait.
Published byMavis Rose Modified over 9 years ago
1
Overview of VBA Programming & Syntax
2
Programming With Objects u Objects –Properties: attributes or characteristics of an object (e.g., font size, color, name, value, visible) –Methods: things an object can do (e.g., calculate, additem, show, hide.) –Events: things that can happen to an object (e.g., click, activate, change, scroll.) u Excel’s Object Model u The VBA Editor
3
Properties: attributes or characteristics of an object.
4
Methods: things an object can do.
5
Events: things that can happen to an object.
6
Variables u Variable - a named location in the computer’s Random Access Memory (RAM) in which we can store data (i.e., text or numbers). u RAM is volatile so data stored there is not permanent. u Examples of variables... X = 7 Lname = “Major” X 7 Lname Major VB: RAM:
7
Do You Get It? Sub Test( ) X = 7 Y = 2 Z = X * Y Msgbox Z End Sub Note: Variables are usually zero or null until assigned a value.
8
The MsgBox Statement u MsgBox prompt [, buttons] [, title] u Example msg = “I like VBA” MsgBox msg,,“My Message”
9
Types of Variables Data typeDescriptionRange Byte 1-byte binary data0 to 255. Integer 2-byte integer< | 32,768 | Long 4-byte integer< |2,147,483,648| Single4-byte floating-pt.-1.4E – 45 to 3.4E38 Double 8-byte floating-pt. – 1.8E308 to 1.79E308 Currency 8-byte, 4 decimals –9E-14 to 9.2E14 String String of characters 0 to two billion chars. Variant 16-byte (chameleon)depends Boolean 2 bytesTrue or False. Date8-byte date/time 1/1/0100 to 12/31/9999. Object 4 bytesAny object reference.
10
Declaring Variables u Use Dim or Static within a Sub or Function Examples… Sub Test( ) Dim X As Integer Dim Y As Single Dim Z As Boolean Dim A, B, C As Currency ‘ A & B are Variant! Static D As Long‘ D will retain its value …….. End Sub Note: All variables in this example are local to this Sub
11
Declaring Variables u Use Public or Private at the Module Level Examples… Private Term As Integer Public Payment As Currency u Public variables in code modules are ‘global’ u Public variables in sheet modules are basically user- defined properties of the sheet
12
Arrays u An array is an indexed set of memory locations. u Each element of the array has a unique index. Example... Dim A(1 to 3) As Single A(1) = 7 A(2) = 19 A(3) = 5 Sum = A(1)+A(2)+A(3) 7 19 5 VB: RAM: A(i) 31 Sum
13
Arrays cont’d u Arrays can also be multi-dimensional Example... Dim A(1 to 3, 1 to 2) As Single A(1,1) = 10 A(3,2) = 13 10 0 0 0 0 13 VB: A(i,j) RAM:
14
Procedures u Two types: Sub(routines) & Functions u They can be either Private or Public (default) u A function always returns a value! u Sub Example… Public Sub Test( ) Dim x As String x = “BIT 5474” Msgbox “This course is ” & x End Sub
15
Procedures u Function Example… Private Function AddThree( x as Single) As Single AddThree = x + 3 End Function u We might use (or call) this function as follows Z = AddThree( 7 )
16
Procedure Arguments u Arguments may be passed to Subs and Functions either ByVal or ByRef Example... Sub MyExample ( ) Dim A as Integer, B as Integer A = 5 : B = 7 TestIt A, B ‘ Here, A becomes 25, B is still 7 End Sub Sub TestIt(ByRef X As Integer, ByVal Y As Integer) X = X^2 : Y = Y^2 End Sub
17
The InputBox Function u InputBox(prompt [, title] [, default] [, xpos] [, ypos] ) u Example msg = “How much would you like to borrow?” x = InputBox(msg, “Amount Financed”) u If the user clicks OK or hits Enter, the InputBox function returns whatever is in the text box. If the user clicks Cancel, the function returns a zero-length string (“”).
18
The MsgBox Function u MsgBox( prompt [, buttons] [, title] ) u Example msg = “Would you like to continue?” If MsgBox(msg, vbYesNo, “Terminate Job?”)=vbYes Then ‘ blah, blah, blah Else ‘ blah, blah, blah End If
19
Decision Structures u If...Then u If...Then...Else u If...Then...ElseIf u Select Case
20
If…Then Examples… If X 2 Then Y = X*Z If Q>Z Then X=5*Q : Y = 2*Z If X > 5 Then Y = X + 25 X = 0 End If
21
If…Then…Else If age < 16 Then prompt = "You are not old enough for a license." MsgBox prompt Else prompt = "You can be tested for a license." MsgBox prompt End If
22
If…Then…ElseIf If jobClass = 1 Then Bonus = salary * 0.1 * rating / 10 ElseIf jobClass = 2 Then Bonus = salary * 0.09 * rating / 10 ElseIf jobClass = 3 Then Bonus = salary * 0.07 * rating / 10 Else Bonus = 0 End If
23
Select Case Select Case jobClass Case 1 Bonus = salary * 0.1 * rating / 10 Case 3 Bonus = salary * 0.07 * rating / 10 Case 2, 4, 5 'The expression list can contain several values... Bonus = salary * 0.05 * rating / 5 Case 6 To 8 '...or be a range of values Bonus = 150 Case Is > 8 '...or be compared to other values Bonus = 100 Case Else Bonus = 0 End Select
24
Looping Structures u Do While...Loop u Do Until...Loop u Do...Loop While u Do...Loop Until u For...Next u For Each...Next
25
Do While…Loop Do While Not( rs.Eof ) ' rs.Eof returns True/False Stocklist.Additem rs(“ticker”) rs.MoveNext Loop
26
Do Until…Loop Response = MsgBox("Do you want more data?", vbYesNo) Do Until Response = vbNo ProcessUserData 'Call procedure to process data Response = MsgBox("Do you want more data?", vbYesNo) Loop
27
Do…Loop While Do ProcessUserData 'Call procedure to process data Response = MsgBox("Do you want more data?", vbYesNo) Loop While Response = vbYes
28
Do…Loop Until Do ProcessUserData 'Call procedure to process data Response = MsgBox("Do you want more data?", vbYesNo) Loop Until Response = vbNo
29
For…Next For i = 1 to 3 Step 1 Sum = Sum + A(i) Next i For i =LBound(A) To Ubound(A) Sum = Sum + A(i) Next i
30
For Each…Next For Each x In Worksheets If x.Name = “Grades” Then X.Delete Next x Note: This is very useful for looping through collections of objects.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.