How to create a VBA function with Several argument as ParamArray or equivalent

Jean Gorsse 1 Reputation point
2021-12-05T16:42:27.663+00:00

I would like to create a VBA Function where I can enter as argument several array, in order to perform matrix calculation on them, like the Excel function MMULT for example.

Developer technologies Visual Basic for Applications
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Tom van Stiphout 1,861 Reputation points MVP Volunteer Moderator
    2021-12-05T20:25:43.927+00:00

    Here is an example. Armed with this and the help file you should be able to figure it out.

    'PROCEDURE:
    '   MaxValue
    'PURPOSE:
    '   Using variants because user can pass in any data type.
    '   Example: myMax = MaxValue(1, 3.5, 5)
    'ARGUMENTS:
    '   Any number of values (not an array)
    'RETURNS:
    '   Variant
    Public Function MaxValue(ParamArray varValue() As Variant) As Variant
        Dim v                   As Variant
        Dim vMax                As Variant
    
        If UBound(varValue) = -1 Then
            vMax = Null        'Zero arguments passed in
        Else
            vMax = varValue(0)
            For Each v In varValue
                If v > vMax Then vMax = v
            Next
        End If
        MaxValue = vMax
    End Function
    
    0 comments No comments

  2. Jean Gorsse 1 Reputation point
    2021-12-05T20:36:23.337+00:00

    Thank, it is not what I was especting but finally I find my answer:

    Function ReturnMMult(Arr1rng As Range, Arr2rng As Range) As Variant

    Dim Arr1() As Variant: Arr1 = Arr1rng.Value
    Dim Arr2() As Variant: Arr2 = Arr2rng.Value
    ReturnMMult = WorksheetFunction.MMult(Arr1, Arr2)

    End Function

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.