VBA Training02

download VBA Training02

of 18

Transcript of VBA Training02

  • 8/14/2019 VBA Training02

    1/18

    Visual Basic for ApplicationBasic Excel Macro (VBA) Orientation Course

  • 8/14/2019 VBA Training02

    2/18

  • 8/14/2019 VBA Training02

    3/18

    Course Objectives:

    To have a basic knowledge on VBA

    To be able to learn the VBA Programming Fundamentals

    To be able to make VBA programs

  • 8/14/2019 VBA Training02

    4/18

    Getting Started with VBA Part 1:

    Before Writing a Program

    -Have a thorough understanding of the problem

    -Carefully plan an approach for solving it

    Algorithm

    -Procedure in terms of:

    actions to be executed

    the orders in which these actions are to be executed

    Pseudocode

    -Artificial, informal language that helps us develop algorithm

    -similar to everyday English

    -not actually executed on Computers

    -help us think out a program before writing it

    Recap

  • 8/14/2019 VBA Training02

    5/18

  • 8/14/2019 VBA Training02

    6/18

    Getting Started with VBA Part 1:

    Variables

    -is an identifier (name) for a storage location (memory) which holds a value-elements you interact with

    -temporary storage of data

    -reference a value in the computer memory

    Age Name : Age

    Data Type : Integer

    Value : 23

    Rules

    -name can contain letter & numbers; it must start with a letter

    -cannot use space & periods

    -not case sensitive

    -cannot be longer than 256 characters

    -cannot use VBA reserved words

    -descriptive

    Constants

    - Variable that cannot be changed

    =23

  • 8/14/2019 VBA Training02

    7/18

    Getting Started with VBA Part 1:

    Declaring Variables

    -tell the Computer to reserve space in memory for later use-Dim (Dimension) Statement

    -have different Data Types

    Option Explicit

    Option Implicit

    -declare all variables using the Dim,Pivate,Public,Redim,Static Statement-error on undeclared variable names

    -avoid incorrect typing of an existing variable name

    -avoid confusion in code where the scope of variable is not clear

    -not using the Dim Statement

    -undeclared variables are of Variant Type

    -makes code harder to interpret & more difficult to debug

    Dim

    Option explicit

    Dim MyVar

    MyInt = 10

    MyVar = 10

    myVaras Integer

    ' Force explicit variable declaration.

    ' Declare variable.

    ' Undeclared variable generates error.

    Declared variable does not generate error.

  • 8/14/2019 VBA Training02

    8/18

  • 8/14/2019 VBA Training02

    9/18

    Getting Started with VBA Part 1:

    -it is good programming style to declare variables

    -it makes the code easier to read for others

    -easy to debug

    -take up less memory

    -code will run faster

    Importance:

    Data Types

    -define the kind of value that may be stored with a memory allocated for a variable

  • 8/14/2019 VBA Training02

    10/18

    Getting Started with VBA Part 1:

    VBA

    Data

    Types

    Common VBA Data Types

  • 8/14/2019 VBA Training02

    11/18

    Getting Started with VBA Part 1:

    VBA Mathematical Operators

    Assigning values to variables

    Assignment #1

    Common VBA Mathematical Operators

    Operator Precedence

    -BODMAS

    -declare 2 variables

    -use the 5 mathematical operators in the program

  • 8/14/2019 VBA Training02

    12/18

    Getting Started with VBA Part 1:

    Subroutines

    -smallest element in the program that can be executed

    -define by the sub keyword

    - executed as a macro in Excel

    Event Procedures

    -triggered by an event

    -VBA predefined procedures

    Sub ProcedureName ( Parameter list)

    [ Statements.}

    End Sub

    Procedures

    -A collection of statements that performs 1 or more given task

    -Types: Subroutine & Functions

  • 8/14/2019 VBA Training02

    13/18

    Getting Started with VBA Part 1:

    Parameter Passing

    -copy the value stored in the accompanying variable

    -does not affect the original value

    ByVal

    ByRef

    -passing the original variable to the procedure

    -does not retain its original value

    Example

    Assignment #2-declare 2 variables

    -use the 5 mathematical operators in the program

    -use parameter passing

    http://passing.xls/http://passing.xls/
  • 8/14/2019 VBA Training02

    14/18

    Getting Started with VBA Part 1:

    Function

    -return a value to procedure

    -Common Excel Functions like Sum, CountA,Count,Vlookup, etc

    -User define functions

    Function FunctionName ( Parameter list) as Type

    [ Function Procedure codes}

    FunctionName =FunctionResults

    End Function

    Example

    Assignment #3-declare 2 variables

    -use the 5 mathematical operators in the program

    -use Function

    http://passing.xls/http://passing.xls/
  • 8/14/2019 VBA Training02

    15/18

  • 8/14/2019 VBA Training02

    16/18

    ting Started with VBA Part 1:

    Basic Input/Output with VBA

    InputBox

    MsgBox

    -outputs a message to the user in a form of a message box

    MsgBox (prompt [,buttons] [,title] [,helpfile, context])

    VBA Help

    -F1

    -returns the data entered by the user

    InputBox (prompt [,title] [,default] [,xpos] [,ypos] [,helpfile, context])

    Assignment #4

    -ask user to input 2 variables using input box

    -use the 5 mathematical operators in the program

    -use Function, use Msgbox to prompt result

    G tti St t d ith VBA P t 1

  • 8/14/2019 VBA Training02

    17/18

    Getting Started with VBA Part 1:

    Program Exercise

    1. Make a program that will compute for the average of 10 numbers

    2. Make a program that will compute for the percentage of a number

    3. Make a program that will convert temperature

    4. Make a program that will convert mm to cm to in

    5. Make any program you want, etc.

  • 8/14/2019 VBA Training02

    18/18

    Chapter 2

    Chapter 3