19
Modeling using VBA

Modeling using VBA

  • Upload
    gasha

  • View
    41

  • Download
    0

Embed Size (px)

DESCRIPTION

Modeling using VBA. Using Toolbox. Using the Toolbox select a GUI element and by mouse-click place it on the frame. This is a label. This is a text box. This is a button. Using User Form Work with Common Button, Text Box. Using User Form Work with Common Button, Text Box. - PowerPoint PPT Presentation

Citation preview

Page 1: Modeling using VBA

Modeling using VBA

Page 2: Modeling using VBA

Using Toolbox

This is a label

This is a button

Using the Toolbox select a GUI element and by mouse-click place it on the frame This is a text box

Page 3: Modeling using VBA

Using User Form Work with Common Button, Text Box

Page 4: Modeling using VBA

Using User Form Work with Common Button, Text Box

Page 5: Modeling using VBA

Working with Combo Box

Page 6: Modeling using VBA

Working with List Box

Page 7: Modeling using VBA

Working with Image Control, Spin Button

Page 8: Modeling using VBA

Work with Multipage, Option controls

Page 9: Modeling using VBA

Work with Scroll Bar, Check Box, Frame controls

Page 10: Modeling using VBA

Work with additional controls

Page 11: Modeling using VBA

Modules & Procedures

• Module – collection of logically related procedures & functions grouped together

• Procedure – a group of ordered statements enclosed by Sub and End Sub

• Function – the same as a procedure, but also returns some value and is enclosed between Function and End Function key words

Page 12: Modeling using VBA

Procedure & Function Examples

Sub ShowTime() Range("C1") = Now()

End Sub

Function sumNo(x, y)     sumNo = x + y

End FunctionFunction: returns something

Procedure: doesn’ t returns anything

Page 13: Modeling using VBA

Calling procedures vs. calling functions

Sub z(a)    MsgBox a

End Sub

Sub x()    Call z("ABC")

End Sub

Sub y()    z "ABC“

End Sub

Sub ShowSum()varSum=  Module1.sumNo(3,5)

MsgBox varSumEnd Sub

Function sumNo(x, y)     sumNo = x + y

End Function

If there are several sumNo functions in several modules/forms, need to use the full name of the function

Page 14: Modeling using VBA

Passing Arguments by Value or by Reference

• Passing arguments by reference – – Is the VBA default– Means, if any changes happened to the argument

variables, they will be preserved after the function/procedure finishes

• Passing arguments by value –– Is possible in VBA (by explicit definition)– Means, the pre-calling state of the argument variables will

be preserved after the procedure/function finishes

Page 15: Modeling using VBA

Arguments by Ref/by Val. Examples

Sub TestPassing1()    Dim y As Integer    y = 50    AddNo1 y    MsgBox y AddNo2 y MsgBox y

End Sub

Sub AddNo1(ByRef x As Integer)    x = x + 10

End Sub

Sub AddNo2(x As Integer)x = x + 10

End Sub

public Sub TestPassing2()    Dim y As Integer    y = 50    AddNo3 y    MsgBox y

End Sub

private Sub AddNo3(ByVal x _ As Integer)    x = x + 10

End Sub

Page 16: Modeling using VBA

Functions/Procedure Scope

• Use public to allow any module to call the function/procedure

• Use private to make limited access to the function/procedure (only from the owning module)

Page 17: Modeling using VBA

VBA Variables

• A variable is used to store temporary information within a Procedure, Function, Module…

• A variable name– Must start with letter and can’t contain spaces and special

characters (such as “&”, “%”, “\”) – Can’t be any excel keyword (“if”, “while”…)– Can’t have identical name to any existing class

(“Worksheet”, “Workbook”…)

Page 18: Modeling using VBA

VBA Data Type

• Byte – positive integer numbers (0:255)• Integer – integers (-32,768 : 32,767)• Long – 4-byte integer• Currency – for fixed-point calculations• Single – 2-byte floating-point numbers• Double – double-precision floating-point numbers• Date – used to store dates and times as real

numbers.• String – contains a sequence of characters

Page 19: Modeling using VBA

Using Variables

• Declaring Variables– Format: Dim varibleName AS dataType– Examples:

• Dim myText As String • Dim myNum As Integer• Dim myObj As Range

– The default value of • any numeric variable is zero• any string variable – “” (empty string)• an Object variable – is nothing