DW Faisalabad New Version

DW Faisalabad New Version
Please Jump to New Version
Showing posts with label Function and Sub. Show all posts
Showing posts with label Function and Sub. Show all posts

Thursday, 13 July 2017

ByRef and ByVal

You can pass arguments to a procedure (function or sub) by reference or by value. By default, Excel VBA passes arguments by reference. As always, we will use an easy example to make things more clear.

Place a command button on your worksheet and add the following code lines:

Dim x As Integer
x = 10

MsgBox Triple(x)
MsgBox x
The code calls the function Triple. It's the result of the second MsgBox we are interested in. Functions need to be placed into a module.

1. Open the Visual Basic Editor and click Insert, Module.

2. Add the following code lines:

Function Triple(ByRef x As Integer) As Integer

x = x * 3
Triple = x

End Function
Result when you click the command button on the sheet:





3. Replace ByRef with ByVal.

Function Triple(ByVal x As Integer) As Integer

x = x * 3
Triple = x

End Function
Result when you click the command button on the sheet:





Explanation: When passing arguments by reference we are referencing the original value. The value of x (the original value) is changed in the function. As a result the second MsgBox displays a value of 30. When passing arguments by value we are passing a copy to the function. The original value is not changed. As a result the second MsgBox displays a value of 10 (the original value)..
Read More »

Volatile Functions

By default, UDF's (User Defined Functions) in Excel VBA are not volatile. They are only recalculated when any of the function's arguments change. A volatile function will be recalculated whenever calculation occurs in any cells on the worksheet. Let's take a look at an easy example to explain this a bit more.

1. Open the Visual Basic Editor and click Insert, Module.

Create a function called MYFUNCTION which returns the sum of the selected cell and the cell below this cell.

2. Add the following code lines:

Function MYFUNCTION(cell As Range)

MYFUNCTION = cell.Value + cell.Offset(1, 0).Value

End Function


3. Now you can use this function, just like any other Excel function.



4. This is a non-volatile function. Non-volatile functions are only recalculated when any of the function's arguments change. Change the value of cell B2 to 8.



5. Now change the value of cell B3 to 11.



Explanation: the non-volatile function is not recalculated when any other cell on the sheet changes.

6. Update the function as follows to make the function volatile:

Function MYFUNCTION(cell As Range)

Application.Volatile
MYFUNCTION = cell.Value + cell.Offset(1, 0).Value

End Function
7. Change the value of cell B3 to 12.

Result:



Note: you need to enter the function again to make it volatile (or refresh it by placing your cursor in the formula bar and pressing enter)..
Read More »

Custom Average Function

Below we will look at a program in Excel VBA which creates a User Defined Function that calculates the average of a randomly selected range excluding one or more values that are outliers and shouldn't be averaged.

Situation:



User defined functions need to be placed into a module.

1. Open the Visual Basic Editor and click Insert, Module.

2. Add the following code line:

Function CUSTOMAVERAGE(rng As Range, lower As Integer, upper As Integer)
The name of our Function is CUSTOMAVERAGE. The part between the brackets means we give Excel VBA a range and two Integer variables as input. We name our range rng, one Integer variable we call lower, and one Integer variable we call upper, but you can use any names.

3. Next, we declare a Range object and two variables of type Integer. We call the Range object cell. One Integer variable we call total and one Integer variable we call count.

Dim cell As Range, total As Integer, count As Integer
4. We want to check each cell in a randomly selected range (this range can be of any size). In Excel VBA, you can use the For Each Next loop for this. Add the following code lines:

For Each cell In rng

Next cell
Note: rng and cell are randomly chosen here, you can use any names. Remember to refer to these names in the rest of your code.

5. Next, we check for each value in this range if it falls between the two values (lower and upper). If true, we increment total by the value of the cell and we increment count by 1. Add the following code lines to the loop.

If cell.Value >= lower And cell.Value <= upper Then
    total = total + cell.Value
    count = count + 1
End If
6. To return the result of this function (the desired average), add the following code line outside the loop.

CUSTOMAVERAGE = total / count
7. Don't forget to end the function. Add the line:

End Function
8. Now you can use this function just like any other Excel function to calculate the average of numbers that fall between two values.

Result:



As a check, you can delete all values that are lower than 10 and higher than 30 and use the standard Average function in Excel to see if Excel calculates the same average as our custom average function.



Our custom average function works! Note: this function is only available in this workbook..
Read More »

User Defined Function

Below we will look at a program in Excel VBA that creates a User Defined Function. Excel has a large collection of functions. In most situations those functions are sufficient to get the job done. If not, you can create your own function called User Defined Function or custom Excel function. You can access a User Defined Function just like any other Excel function.

We want to create a function called SUMEVENNUMBERS that finds the sum of the even numbers of a randomly selected range.

Situation:



User defined functions need to be placed into a module.

1. Open the Visual Basic Editor and click Insert, Module.

2. Add the following code line:

Function SUMEVENNUMBERS(rng As Range)
The name of our Function is SUMEVENNUMBERS. The part between the brackets means we give Excel VBA a range as input. We name our range rng, but you can use any name.

3. Next, we declare a Range object and call it cell.

Dim cell As Range
4. We want to check each cell in a randomly selected range (this range can be of any size). In Excel VBA, you can use the For Each Next loop for this. Add the following code lines:

For Each cell In rng

Next cell
Note: cell is randomly chosen here, you can use any name.

5. Next, we check for each value in this range whether it is even or not. We use the Mod operator for this. The Mod operator gives the remainder of a division. So 7 mod 2 would give 1. 7 is divided by 2 (3 times) to give a remainder of 1. Having said this, it is easy to check whether a number is even or not. Only if the remainder of a number divided by 2 is 0, the number is even. 8 mod 2 gives 0, 8 is divided by 2 exactly 4 times, and therefore 8 is even. Add the following If statement to the For Each Next loop.

If cell.Value Mod 2 = 0 Then

End If
6. Only if this statement is true, we add the value to SUMEVENNUMBERS. Add the following code line in the If statement.

SUMEVENNUMBERS = SUMEVENNUMBERS + cell.Value
7. Don't forget to end the function (outside the loop).

End Function
8. Now you can use this function, just like any other Excel function, to find the sum of the even numbers of a randomly selected range.

Result:



Well done! That's how easy User Defined Functions in Excel VBA are. Note: this function is only available in this workbook..
Read More »

Function and Sub

Function  |  Sub

The difference between a function and a sub in Excel VBA is that a function can return a value while a sub cannot. Functions and subs become very useful as program size increases.

Function

If you want Excel VBA to perform a task that returns a result, you can use a function. Place a function into a module (In the Visual Basic Editor, click Insert, Module). For example, the function with name Area.

Function Area(x As Double, y As Double) As Double

Area = x * y

End Function

Explanation: This function has two arguments (of type Double) and a return type (the part after As also of type Double). You can use the name of the function (Area) in your code to indicate which result you want to return (here x * y).

You can now refer to this function (in other words call the function) from somewhere else in your code by simply using the name of the function and giving a value for each argument.

Place a command button on your worksheet and add the following code lines:

Dim z As Double

z = Area(3, 5) + 2

MsgBox z
Explanation: The function returns a value so you have to 'catch' this value in your code. You can use another variable (z) for this. Next, you can add another value to this variable (if you want). Finally, display the value using a MsgBox.

Result when you click the command button on the sheet:



Sub

If you want Excel VBA to perform some actions, you can use a sub. Place a sub into a module (In the Visual Basic Editor, click Insert, Module). For example, the sub with name Area.

Sub Area(x As Double, y As Double)

MsgBox x * y

End Sub
Explanation: This sub has two arguments (of type Double). It does not have a return type! You can refer to this sub (call the sub) from somewhere else in your code by simply using the name of the sub and giving a value for each argument.

Place a command button on your worksheet and add the following code line:

Area 3, 5
Result when you click the command button on the sheet:



Can you see the difference between the function and the sub? The function returned the value 15. We added the value 2 to this result and displayed the final result. When we called the sub we had no more control over the result (15) because a sub cannot return a value!.
Read More »