t ransaction p rocessing using lookups and macros to prepare bills man 327 2.0 micro-computers &...
TRANSCRIPT
Kasu
n@
sjp.a
c.lk
1
TRANSACTION PROCESSING
Using lookups and macros to prepare Bills
MAN 327 2.0 Micro-computers & Their Applications
2
Kasu
n@
sjp.a
c.lk
VLOOKUP
Search for a value in the leftmost column of a table, and returns appropriate value in another column.
Table must be sorted in ascending order
3
Kasu
n@
sjp.a
c.lk
SAMPLE DATABASE
Code Product Price
A23 Paper 5.00
B14 Lamp 15.00
A27 Desk 75.00
C45 Pencil 0.50
4
Kasu
n@
sjp.a
c.lk
VLOOKUP
VLOOKUP(A7, $A$2:$C$5,2, FALSE)
Search
What? Search
Where?
What you
Want?
Exact match or
not
5
Kasu
n@
sjp.a
c.lk
A27 75.00 exact match
A27 5.00 approximate match
A28 #N/A VLookup -- not found
A28 IF and VLOOKUP
6
Kasu
n@
sjp.a
c.lk
=VLOOKUP(A7,$A$2:$C$5,2,FALSE) Since 123 is a text, VLookup formula returns #N/A
=VLOOKUP(--A8,$A$2:$C$5,2,FALSE) Double unary (--) converts text lookup value to number
=VLOOKUP(--A9,$A$2:$C$5,2,FALSE) The same formula works if the lookup value is a
number
123 #N/A123 Paper123 Paper
7
Kasu
n@
sjp.a
c.lk
HOW TO AVOID #N/A
=if( ISNA (VLOOKUP(A7,$A$2:$C$5,2,FALSE), ””, =VLOOKUP(A7,$A$2:$C$5,2,FALSE))
- if not found, print “”
=IFERROR(VLOOKUP(B6,OrdersA,2,FALSE),IFERROR(VLOOKUP(B6,OrdersB,2,FALSE), "Not Found"))
- find elsewhere
Formulas Name Manager
8
Kasu
n@
sjp.a
c.lk
REPORT CARD USING LOOKUP
Score Grade0 F
50 D60 C70 B85 A
Subject Score GradeEnglish 77 BMath 67 CPhysics 86 A
=VLOOKUP(B4,GradeList,2)
10
Kasu
n@
sjp.a
c.lkAUTOMATING A WORKBOOK WITH MACROS
A macro enables you to combine multiple
commands and keystrokes and execute
them as a single command.
If you perform a particular task frequently,
creating a macro to use can be time-saving and
improves the consistency and accuracy of
repetitive procedures.
11
Kasu
n@
sjp.a
c.lk
MACROS IN EXCEL 2007
Displaying the developer tab Office>Excel Options Popular Check Show Developer tab in the Ribbon
Macro security
12
Kasu
n@
sjp.a
c.lkRECORDING MACROS IN EXCEL 2007 Developer>Code>Record Macro Click the Record Macro button in the status
bar Type the name of the macro in the input box Save in “This workbook” Click “Stop recording” when finished
13
Kasu
n@
sjp.a
c.lk
RECORDING MACROS TIPS
Think what operations you want to include in the macro.When to startWhen to stop If it will include any formatting
Use a short but descriptive name so that you recognise it later
In Excel 97-2003, if “Stop recording” does not appear, use Tools | Macro | Stop recording
14
Kasu
n@
sjp.a
c.lk
RECORDING MACRO TIPS (2)
Remember to stop the recording. Trying to run the macro, without stopping recording, will result to a macro that will continue endlessly.
If you want to use the macro in more than one workbook, save it in the Personal Macro Workbook.
Absolute recording is the default action. Relative recording remembers the
position of the active cell relative to its previous position.
15
Kasu
n@
sjp.a
c.lk
RUNNING MACROS Use Tools | Macro | Macros…on the
Worksheet menu and double click the macro name. In Excel 2007 use the macros button in the
Developer tab. Alternatively, use the shortcut key that you
assigned. Macros are available to all open workbooks
16
Kasu
n@
sjp.a
c.lk
VISUAL BASIC EDITOR
Project explorer
Properties window
Code window
Immediate window
17
Kasu
n@
sjp.a
c.lk
THINKING IN TERMS OF OBJECTS
Objects are Excel elements that can be manipulated using VBA.Application object (Excel)Workbooks (all Workbook objects)
Worksheets (all Worksheet objects) Range PageSetup
Charts (all Chart objects) Names (all Name objects)
Windows (all Window objects)Addins (all Addin objects)
18
Kasu
n@
sjp.a
c.lk
WORKING WITH OBJECTS USING VBA
Objects can be manipulated using the collections they belong to, specifying their location in the object hierarchy using the dot notation.Worksheets(“Sheet1”)Worksheets(1)Workbooks(“Book1”).Worksheets(“Sheet1”
)Worksheets(“Sheet1”).Range(“A1”)Application.Workbooks(“Book1”)._
Worksheets(“Sheet1”).Range(“A1”)
19
Kasu
n@
sjp.a
c.lkACCESSING PROPERTIES AND METHODS
Properties
Reading
Worksheets(“Sheet1”).Range(“A1”).Value
Setting
Range(“A1”).Value=123
Methods
Range(“A1:D5”).Clear
20
Kasu
n@
sjp.a
c.lk
RANGE OBJECTS Multiple representations
Different thing in different cases Single cell
Collection of cells
The handle to the thing you want to work with
21
Kasu
n@
sjp.a
c.lk
REFERRING TO RANGE OBJECTS
Using the Range property
Applies to:
Application object
Worksheet object
Range object
Cells property of a Worksheet object
Offset property of a Range object
22
Kasu
n@
sjp.a
c.lk
RANGE PROPERTY Object.Range(cell1) Object.Range(cell1, cell2) Worksheets(“Sheet1”).Range(“A1”). _
Value = 1 Range(“Input”).Value = 5 Range(“A1:B10”).Value=3 Range(“A1”, “B10”)=3 Range(“C1:C10 A6:E6”)=5 Range(“A1, A5, B4, F5”)=6
23
Kasu
n@
sjp.a
c.lk
CELLS PROPERTY
Worksheets(“Sheet1”).Cells(1,1)=5
Changes the value of A1
Cells(5,2) = 4
Changes the value of B5
Cells(257) = 6 (Cells(16385)=6) for earlier
versions only
Changes cell A2
256 cells per row (16384)
65536 rows (1,048,576)
Last cell is 16,777,216 (17,179,869,184)
24
Kasu
n@
sjp.a
c.lk
CELLS PROPERTY (2)
With range objects
Range(“A1:B10”).Cells(4) = 400
Will change the value of B2
Worksheets(“Sheet1”).Cells.ClearCont
ents
25
Kasu
n@
sjp.a
c.lk
OFFSET PROPERTY
Applies only to the Range object Range(“B1”).Offset(1,0).Value=4
Changes B2 value to 4 Range(“C6”).Offset(-4,-2).Value = 5
Changes A2 value to 5
Exercise: Make another version of the macro using the Offset property
26
Kasu
n@
sjp.a
c.lk
VARIABLES
Variable is a named storage location in the
computer’s memory
Can accommodate wide variety of data types
28
Kasu
n@
sjp.a
c.lk
VARIABLES (2)
It is better to determine the data type of a
variable. Your applications will be executing
much faster!
Use Option Explicit to force yourself to
declare the variables you use
Constant is a “variable” that retains its value
Const companyName as String = “City
University”
29
Kasu
n@
sjp.a
c.lk
VARIABLE’S LIFETIME AND SCOPE
Two important concepts associated with variables:Scope
Defines which procedures can use that variable
LifetimeDefines how long that variable retains the values assigned to it
30
Kasu
n@
sjp.a
c.lk
VARIABLE LIFETIME
The lifetime of the variable depends where it is declared In a procedure
The lifetime is limited to the time taken to run the procedure
In a procedure using the Static keyword The lifetime is extended to the time that the
workbook is open In the declarations section
The lifetime is extended to the time that the workbook is open
31
Kasu
n@
sjp.a
c.lk
VARIABLE SCOPE
The scope of the variable depends where it is declared:In a procedure
It is visible only within the procedure code
In a moduleIt is visible only within the module code
In a module as PublicIt is visible from all open workbooks
In a module as PrivateIt is visible only within the module code
32
Kasu
n@
sjp.a
c.lk
INPUT AND OUTPUT Output
Using MsgBox functionMsgBox (“This is a message”)
InputUsing InputBox function
variableName = InputBox(“Enter a value”)
There are two Inputbox functions. VBScript Application object
Exercise: Make a subroutine that calculates the summation of two integers taken by user input and prints them using a message box
33
Kasu
n@
sjp.a
c.lkMANIPULATING OBJECTS AND COLLECTIONS For each – Next constructs
Use it when you want to process every member of a collection
When you don’t know how many objects are in a collection
34
Kasu
n@
sjp.a
c.lk
CONTROL STRUCTURES
If – then constructsMost commonly usedSyntax:If condition Then
condition_true_instructions[Else If condition2 Then
condition2_true_instructionsElse
both_conditions_false_instructions]End If
35
Kasu
n@
sjp.a
c.lk
TYPE CONVERSION TECHNIQUES
Convert an expression to a specific data type Conversion functions include Boolean, Byte,
Currency, Date, Double, Decimal, Integer, Long, Single, String, Variant
CBool, CByte, CCur, CDate, CDbl, CDec, CInt, CLng, CSng, CStr, CVar
37
Kasu
n@
sjp.a
c.lk
CELL REFERENCES (1 OF 2)
Relative versus Absolute References Critical to specify whether cell references are
absolute or relative Absolute cell addresses are constant
Macro refers to that cell every time the macro is run Relative cell addresses change
VBA uses offset to indicate space from active cell every time the macro is run
38
Kasu
n@
sjp.a
c.lk
CELL REFERENCES (2 OF 2)
Relative references ActiveCell.Offset(1,0).Range(“A1”).Select Relative reference above means the cell one row
below the active cell Offset of (0,1) means the cell one column to the
right of the active cell Negative numbers are used for cells above or to
the left of the active cell Absolute references
Range(“A1”).Select Always refers to cell A1
39
Kasu
n@
sjp.a
c.lk
Sub dis()
Dim a
a = InputBox("Enter discount rate", "My Title",
10)
MsgBox ("You hae entered " & a / 100)
ActiveCell.FormulaR1C1 = "=R[-5]C[-1] * " & a /
100
ActiveCell.Offset(1, 0).Range("A1").Select
Selection.Font.Bold = True
‘Selection.Font.ColorIndex = 3
End Sub
41
Kasu
n@
sjp.a
c.lk
Option Explicit Global Variable Data Types
Dim VarA As IntegerDim VarB, VarC As Double
Kasu
n@
sjp.a
c.lk
42
OPERATORS (1)
Arithmetic operators: Addition: + Subtraction: - Multiplication: * Division: / Integer Division: \ Modulo Division: MOD Exponentiation: ^ Negative Number: -
String operator: Concatenation (i.e.,
joining two strings together to form a longer string): &
Comparison operators: Equal To: = Not Equal To: <> Less Than: < Greater Than: > Less Than Or Equal To: <= Greater Than Or Equal To: >=
Logical operators: Conjunction: And Disjunction: Or Negation: Not
Commonly used operators
43
Kasu
n@
sjp.a
c.lkOPERATORS (2)
Syntax of some operators: 87 \ 10 ( = 8, result is an integer ) 87 MOD 10 ( = 87 - (87 \ 10) = 7, result is an
integer ) 10^2 ( = 102 = 100 ) "Hello " & "World" ( = "Hello World" )
You should pay attention on the result data types of operators, and functions.
44
Kasu
n@
sjp.a
c.lkOPERATOR PRECEDENCE (1)
^- (negative number)
*, /\MOD+, -&
<, <=, >, >=, =, <> (comparison)NotAndOr
45
Kasu
n@
sjp.a
c.lkOPERATOR PRECEDENCE (2)
1 ^ 2 + 3 = 4 (i.e. 12 + 3 = 4) 1 ^ (2 + 3) = 1 (i.e. 1(2+3) = 1) You need to add parentheses as necessary
47
Kasu
n@
sjp.a
c.lk
IF THEN ELSE
Dim a a = ActiveCell.FormulaR1C1 ActiveCell.Range("A2").Select If a > 50 Then ActiveCell.Value = "Pass" Else ActiveCell.Value = "Fail" End If
48
Kasu
n@
sjp.a
c.lk
SELECT CASE
Dim a, g
a = ActiveCell.FormulaR1C1
ActiveCell.Range("A2").Select
Select Case a
Case Is >= 75
g = "A"
Case Is >= 65
g = "B"
Case Is >= 50
g = "C"
Case Is >= 35
g = "S"
Case Else
g = "F"
End Select
ActiveCell.Value = g
49
Kasu
n@
sjp.a
c.lk
DO WHILE LOOPActiveCell.Offset(1, 0).Range("A1").Select Dim a, s s = 0 a = 0 Do While a < 10 s = s + a a = a + 1 Loop ActiveCell.FormulaR1C1 = s