Finance

Charts

Statistics

Macros

Search

Using range references as parameters for user-defined functions

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).

0 0 votes
Évaluation de l'article
S’abonner
Notification pour
guest
0 Commentaires
Le plus ancien
Le plus récent Le plus populaire
Online comments
Show all comments
Facebook
Twitter
LinkedIn
WhatsApp
Email
Print
0
We’d love to hear your thoughts — please leave a commentx