Download presentation
Presentation is loading. Please wait.
1
Modeling using VBA
2
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
3
Using User Form Work with Common Button, Text Box
5
Working with Combo Box
6
Working with List Box
7
Working with Image Control, Spin Button
8
Work with Multipage, Option controls
9
Work with Scroll Bar, Check Box, Frame controls
10
Work with additional controls
11
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
12
Procedure & Function Examples Sub ShowTime() Range("C1") = Now() End Sub Function sumNo(x, y) sumNo = x + y End Function Function: returns something Procedure: doesn’ t returns anything
13
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 varSum End 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
14
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
15
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
16
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)
17
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 ”… )
18
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
19
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.