Excel VBA Classic Excel VBA 2010 Excel VBA 365 Excel VBA Examples About Us

Lesson 5: Sub Procedure and Functions

5.1: Sub Procedure

A Sub procedure in Excel VBA is a procedure that is called by the main program to perform a certain task. Upon completion of the task, It returns control to the calling code, but it does not return a value. A Subroutine begins with a Sub statement and ends with an End Sub statement.

The program structure of a sub procedure is as follows:

Sub SubProg(arguments)

End Sub

Sub procedure are called by the main program using the Call keyword.

Sub MainProg( )

Call SubProg()

End Sub

A sub procedure is different from a function that it does not return a value directly. You can include parameters in a sub procedure.

Example 5.1

In this example, the main program calls the sub procedure findHidden and execute it. The end result is a message box that display the hidden text.

Private Sub CommandButton1_Click()
Call findHidden
End Sub
Sub findHidden()
hidden_txt = "@#%43&*"
MsgBox hidden_txt
End Sub

Example 5.2

Private Sub CommandButton1_Click()
  Call salary(10, 300)
End Sub

Sub salary(wage As Single, hours As Single)
 MsgBox wage * hours
End Sub

In this example, the Call command calls the subroutine salary and passes the parameters 10 and 300 to it. It will calculate the salary based on wage per hour and number of hours and display on the message box.

5.2 Functions

In Excel VBA, a function is similar to a sub 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, or simply called user-defined functions. The first built-in function that we have already learned and familiar with its usage is the Message Box. We are not going to repeat here but we shall take a look at its syntax once more, i.e.

message=MsgBox(Prompt, Style Value,Title)

Now we shall examine the next commonly used function in Excel VBA, the InputBox function.

5.2.1 InputBox function

An InputBox( ) function displays a message box where the user can enter a value or a message in the form of text. The syntax is

myMessage=InputBox(Prompt, Title, default_text, x-position, y-position)

myMessage is a variant data type but typically it is declared as string, which accept the message input by the users. The arguments are explained as follows:

Example 5.1

In this example, we insert a label and a command button into the MS Excel spreadsheet. Double click on the command button and enter the Excel VBA code as follows:

Private Sub CommandButton1_Click()
Dim userMsg As String
userMsg = InputBox("What is your message?", "Message Entry Form", "Enter your messge here", 500, 700)
If userMsg <> "" Then
    MsgBox( userMsg)
    MsgBox("No Message")
End If
End Sub

* The InputBox is shown in Figure 5.1

Figure 5.1: InputBox

5.2.1 Creating User-Defined Functions

The syntax to create a User-Defined function is as follows:

Public Function functionName (Arg As dataType,...) As dataType


Private Function functionName (Arg As dataType,...) As dataType

Public indicates that the function is applicable to the whole project while Private indicates that the function is only applicable to a certain module or procedure.

In order to create a user-defined function in Excel VBA, you need to go into the Visual Basic Editor in MS Excel Spreadsheet.In the Visual Basic Editor, click on Insert on the menu bar to insert a module into the project. Enter the following code as shown in Figure 5.2. This function is to calculate the cube root of a number.

Figure 5.2

Now enter the function CubeRoot just like you enter the formula of MS Excel, as shown in Figure 5.3. The value of cube root for the number in cell C4 will appear in cell D4.

Figure 5.3

We will deal with more user-defined functions in future lessons

❮ Previous Lesson Next Lesson ❯

Copyright ® 2008 Dr.Liew Voon Kiong . All rights reserved   [Privacy Policy]

Contact: Facebook Page