The previous example of calculating the cost of a batch of books is already quite convincing. VBA can indeed make a user’s life easier.
One question arises from the two examples given. In the user-defined functions created, the parameter values were only cell references. Is it possible to create a user-defined function where the parameter values can be references to a range of cells?
Function MySum(ByVal rng As Range) As Double Dim c As Range Dim s As Double s = 0 For Each c In rng.Cells s = s + c.Value Next MySum = s End Function
The code demonstrates the use of range references and solves the task of summing values in a specified range of cells (see also the file 1-UserFunctions.xlsm on the CD).