Today we will read on Excel vba user defined function – Excel has a large collection of functions. In most situations they are sufficient to work. If not, you can create your own function called a user defined function or a custom Excel function. You can access a user defined function like any other Excel function.
We want to create a function named SUMEVENNUMBERS, which shows the sum of even numbers of a randomly selected range.
User defined functions need to be placed into a module.
-
- Open the Visual Basic Editor and click Insert, Module -> Add the following code line: or download the demo excel file
Option Explicit Function SUMEVENNUMBERS(rng As Range) Dim cell As Range For Each cell In rng If cell.Value Mod 2 = 0 Then SUMEVENNUMBERS = SUMEVENNUMBERS + cell.Value End If Next cell End Function