Custom Average Function on Array Failing on NULL Values

rkaczano

Board Regular
Joined
Jul 24, 2013
Messages
141
Office Version
  1. 365
Platform
  1. Windows
I have an Average function that is averaging an array based on a column. The array has a mix of values and NULL values. I know it is failing as I can trap the error.,

My goal is to only average the non-NULL values in that array.

Can anyone provide me with some insight into how to treat the NULL values.

VBA Code:
Function AverageFunction(DataArray As Variant, column As Integer, year As Integer)
Dim X As Integer
Dim rowcount As Integer
Dim tempArray() As Variant


rowcount = UBound(DataArray, 1)

ReDim Preserve tempArray(1 To rowcount)

'Loop through rows of desired column in given year
For X = 1 To rowcount
    tempArray(X) = DataArray(X, column, year)
    Debug.Print DataArray(X, column, year)
Next

On Error GoTo check:
AverageFunction = Application.Average(tempArray, 1)

check: Debug.Print "Error"

End Function
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
What is it that you are defining as a NULL value?
 
Upvote 0
I am literally filling a 3d arr within an If stmt saying if a condition is TRUE Then fill arr with the a value tied to a class object property that has been defined as a Double. Otherwise fill arr with NULL. I then pass arr to the Average function and perform an average on a column as defined by the column variable I pass to the average function. The result of the average is then passed to a second 2d array called arr2.

Both arr and arr1 are defined as variants (e.g. Dim arr () as Variant)

This all worked when I was simply filing arr with obj.value. But when I added the conditional logic with the NULL nuance, the average function started failing.

If var1 = True Then
arr(Count1, column, Count1) = obj1.value
Else
arr(Count1, column, Count1) = Null
End If

arr2(Count1, column) = AverageFunction(arr, column, Count1)
 
Upvote 0
Do you need to use Null? If, for example, you used a string value like "n/a" instead, the average function would ignore it and average the numbers.
 
Upvote 0

Forum statistics

Threads
1,214,636
Messages
6,120,664
Members
448,976
Latest member
sweeberry

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