Friday, April 30, 2010

Creating VBA Functions For MS Excel

15.1 The Needs to Create VBA Functions in MS-Excel
You can create your own functions to supplement the built-in functions in Microsoft Excel spreadsheet, which are quite limited in some aspects. These user-defined functions are also called Visual Basic for Applications functions, or simply VBA functions. They are very useful and powerful if you know how to program them properly. One main reason we need to create user defined functions is to enable us to customize our spreadsheet environment for individual needs. For example, we might need a function that could calculate commissions payment based on the sales volume, which is quite difficult if not impossible by using the built-in functions alone. The code for VBA is illustrated on the right.
Table 15.1: Commissions Payment Table
Sales Volume($)
Commissons
<500
3%
<1000
6%
<2000
9%
<5000
12%
>5000
15%

Wednesday, April 28, 2010

Creating User-Defined Functions

14.1 Creating Your Own FunctionThe general format of a function is as follows:
Public Function functionName (Arg As dataType,..........) As dataType
or
Private Function functionName (Arg As dataType,..........) As dataType
* Public indicates that the function is applicable to the whole project and
Private indicates that the function is only applicable to a certain module or procedure. 

Example 14.1
In this example, a user can calculate the future value of a certain amount of money he has today based on the interest rate and the number of years from now, supposing he will invest this amount of money somewhere .The calculation is based on the compound interest rate.
 

Saturday, April 24, 2010

Formatting Functions

Formatting output is a very important part of programming so that the data can be presented systematically and clearly to the users. Data in the previous lesson were presented fairly systematically through the use of commas and some of the functions like Int, Fix and Round. However, to have better control of the output format, we can use a number of formatting functions in Visual basic.
The three most common formatting functions in VB are Tab, Space, and Format 

(i) The Tab function
Tab (n); x
The item x will be displayed at a position that is n spaces from the left border of the output form. There must be a semicolon in between Tab and the items you intend to display (VB will actually do it for you automatically).

Example1
Private Sub Form_Activate

Tuesday, April 20, 2010

String Manipulation Functions

In this lesson, we will learn how to use some of the string manipulation function such as Len, Right, Left, Mid, Trim, Ltrim, Rtrim, Ucase, Lcase, Instr, Val, Str ,Chr and Asc.

(i)The Len Function 
The length function returns an integer value which is the length of a phrase or a sentence, including the empty spaces. The format is 
Len (“Phrase”) 

For example, 
Len (VisualBasic) = 11 and Len (welcome to VB tutorial) = 22 

The Len function can also return the number of digits or memory locations of a number that is stored in the computer. For example, 

Friday, April 9, 2010

Mathematical Functions

The mathematical functions are very useful and important in programming because very often we need to deal with mathematical concepts in programming such as chance and probability, variables, mathematical logics, calculations, coordinates, time intervals and etc. The common mathematical functions in Visual Basic are Rnd, Sqr, Int, Abs, Exp, Log, Sin, Cos, Tan , Atn, Fix and Round.
(i) Rnd is very useful when we deal with the concept of c

Wednesday, April 7, 2010

Looping

Visual Basic allows a procedure to be repeated many times as long as the processor until a condition or a set of conditions is fulfilled. This is generally called looping . Looping is a very useful feature of Visual Basic because it makes repetitive works easier. There are two kinds of loops in Visual Basic, the Do...Loop and the For.......Next loop 

9.1 Do Loop
The formats are
a) Do While condition
Block of one or more VB statements
Loop
b) Do
Block of one or more VB statements
Loop While condition
c) Do Until condition
Block of one or more VB statements
Loop
d) Do
Block of one or more VB statements
Loop Until condition


9.2 Exiting the Loop
Sometime we need exit to exit a loop prematurely because of a certain condition is fulfilled. The syntax to use is known as Exit Do. You can examine Example 9.2 for its usage. 


9.3 For....Next Loop
The format is:

For counter=startNumber to endNumber (Step increment)

One or more VB statements
Next
Please refer to example 9.3a,9.3b and 9.3 c for its usage.Sometimes the user might want to get out from the loop before the whole repetitive process is executed, the command to use is Exit For. To exit a For….Next Loop, you can place the Exit Forstatement within the loop; and it is normally used together with the If…..Then… statement. Let’s examine example 9.3 d. 


Example 9.1
Do while counter <=1000
num.Text=counter
counter =counter+1
Loop
* The above example will keep on adding until counter >1000.
The above example can be rewritten as
Do
num.Text=counter
counter=counter+1
Loop until counter>1000 


Example 9.2
Dim sum, n As Integer
Private Sub Form_Activate()
List1.AddItem "n" & vbTab & "sum"
Do
n = n + 1
Sum = Sum + n
List1.AddItem n & vbTab & Sum
If n = 100 Then
Exit Do
End If
Loop
End Sub
 

Explanation
In the above example, we compute the summation of 1+2+3+4+……+100. In the design stage, you need to insert a ListBox into the form for displaying the output, named List1. The program uses the AddItem method to populate the ListBox. The statement List1.AddItem "n" & vbTab & "sum" will display the headings in the ListBox, where it uses the vbTab function to create a space between the headings n and sum.

Example 9.3 a
For  counter=1 to 10 
display.Text=counter
  Next
Example 9.3 b
For counter=1 to 1000 step 10  
counter=counter+1
 Next
Example 9.3 c
  For counter=1000 to 5 step -5
  counter=counter-10
   Next
*Notice that increment can be negative
Example 9.3 d
Private Sub Form_Activate( )
For n=1 to 10
If n>6 then
Exit For
End If
Else
Print n
End If
End Sub

Saturday, April 3, 2010

Introduction to VB Built-in Functions

A function is similar to a normal procedure but the main purpose of the function is to accept a certain input from the user and return a value which is passed on to the main program to finish the execution. There are two types of functions, the built-in functions (or internal functions) and the functions created by the programmers
the general format of a function is
FunctionName (arguments)
The arguments are values that are passed on to the function.
In this lesson, we are going to learn two very basic but useful internal functions of Visual basic , i.e. the MsgBox( ) and InputBox ( ) functions. You can also learn about mathematical functions, formatting functions and string manipulation functions by clicking the links at the end of this page.