190 likes | 202 Views
Learn how to use VBA and GUI toolbox to create interactive interfaces and work with common controls like buttons, text boxes, combo boxes, and more. Understand the basics of functions, procedures, passing arguments, variables, and data types in VBA programming.
E N D
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
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
Procedure & Function Examples Sub ShowTime()Range("C1") = Now() End Sub Function sumNo(x, y) sumNo = x + y End Function Procedure: doesn’ t returns anything Function: returns something
Calling procedures vs. calling functions If there are several sumNo functions in several modules/forms, need to use the full name of the function 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
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
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
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)
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”…)
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
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