How to use Paramarray with arguments in pairs?

Kelvin Stott

Active Member
Joined
Oct 26, 2010
Messages
338
Hello,

I want to create a VBA function to get the weighted average value of an unspecified number of data points, where each data point is given a certain weight, for example

Function WtdAve(Data1, Weight1, Data2, Weght2, ..., etc.)

I want to use Paramarray where each data point and its weight is given as a pair, but I am not sure how to do this. Could anyone please help point me in the right direction with an example.

Many thanks,
Kelvin
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
You may want to consider storing the parameters Data1, Weight1.... as arrays and then just passing the array as the parameter. For example:

Function WtdAve(Data(), Weight())

This would allow the "Pairs" to be of almost any length.

In the function, you can determine how may pairs were transfer by using the ubound(Data) funtion.
 
Upvote 0
First thing, is you have to give your parameter array a name and place that into your function's argument list (I'll use DataWeights for the name, but you can change that if you want)...

Code:
Function WtdAve(ParamArray DataWeights()) As Double

Next, your code would need to do is make sure an even number of arguments were passed into the parameter array...

Code:
   If (UBound(DataWeights) - LBound(DataWeights)) Mod 2 = 0 Then
    '
    '  An odd number of arguments for the parameter array were
    '  passed in, so put the code, if any, to handle the error here
    '
    Exit Function
  End If
And then finally, you have to process the arguments in the parameter array two at a time (here I am assuming X is the loop counter variable)...

Code:
  For X = LBound(DataWeights) To UBound(DataWeights) Step 2
    '
    '  Here is where you process the arguments from the parameter array. On each
    '  loop, DataWeights(X) will contain the Data item and DataWeights(X + 1) will
    '  contain the Weight for that Data iteme
    '
  Next
 
Upvote 0
Thank you Rick,

Not only did you provide the solution to my original problem, you also solved problems I had not yet envisaged.

Cheers,
Kelvin
 
Upvote 0

Forum statistics

Threads
1,222,828
Messages
6,168,484
Members
452,193
Latest member
Arlochorim

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top