My Function is not working

ilya2004

Board Regular
Joined
Mar 17, 2011
Messages
135
Hello all,

I am trying to develop a function that will give me the average of some numbers in a range that meet certain criteria. Here is the beginning of my code:

Code:
Function AverageBetween(Range As Range, LB As Double, UB As Double, Optional Criteria_Range As Range) As Long
Dim Mycel, SearchRange As Range
Dim n As Long
Dim i As Integer
i = 0

n = 0

Set SearchRange = Range.SpecialCells(xltypeformulas, xlNumbers)
For Each Mycel In SearchRange

If Mycel.Text >= LB And Mycel.Text <= UB Then
n = n + Mycel.Text
i = i + 1
End If
Next

averagebetween = n / i

Debug.Print n
Debug.Print i
Debug.Print averagebetween

End Function
It works fine in execution and I even get the correct answer in the immediate window, but when I call the function in a spreadsheet
Code:
=PERSONAL.XLSB!AvergeBetween(B49:B54,B50,B52)
for some reason I get "#name?" as the result in my cell.


Please help!!
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
I modified my code, here is an update:

Code:
Function AverageBetween(MyRange As Range, LB As Double, UB As Double, Optional Criteria_Range As Range)
Dim Mycel, SearchRange As Range
Dim n, i, Avg As Long
i = 0
n = 0



Set SearchRange = MyRange.SpecialCells(xlCellTypeFormulas, xlNumbers)
For Each Mycel In SearchRange
Debug.Print Mycel.Row
If Mycel.Text >= LB And Mycel.Text <= UB Then
n = n + Criteria_Range(Mycel.Row, 1)
i = i + 1
End If
Next



AverageBetween = n / i
Debug.Print n
Debug.Print i
Debug.Print Avg


End Function
 
Upvote 0
Code:
=PERSONAL.XLSB!Aver[COLOR=red]a[/COLOR]geBetween(B49:B54,B50,B52)

I wouldn't use a VBA keyword (Range) as a variable name.
 
Upvote 0
In addition to the typo that has already been pointed out to you, there are certain Excel functions/methods/properties that do not work in a user defined function. Maybe, SpecialCells is one of them.

You may wish to loop through each cell in the range and check if it contains a formula or not and if so then whether the value is a number or not.
I modified my code, here is an update:

Code:
Function AverageBetween(MyRange As Range, LB As Double, UB As Double, Optional Criteria_Range As Range)
Dim Mycel, SearchRange As Range
Dim n, i, Avg As Long
i = 0
n = 0



Set SearchRange = MyRange.SpecialCells(xlCellTypeFormulas, xlNumbers)
For Each Mycel In SearchRange
Debug.Print Mycel.Row
If Mycel.Text >= LB And Mycel.Text <= UB Then
n = n + Criteria_Range(Mycel.Row, 1)
i = i + 1
End If
Next



AverageBetween = n / i
Debug.Print n
Debug.Print i
Debug.Print Avg


End Function
 
Upvote 0
I tried to remove the special cells and I fixed the typos and I am still getting zero as a result. I know that the issue is in the n's, for some reason it is not catching what the value is, so it leave it as zero.

Code:
Here is the full code:

    Function AverageBetween(MyRange As Range, _
    LB As Double, UB As Double, Optional Criteria_Range As Range)
    
    Dim Mycel, SearchRange As Range
    Dim n, i, Avg
    i = 0
    n = 0
    
       
    Set SearchRange = MyRange.SpecialCells(xlCellTypeFormulas, xlNumbers)
    
    For Each Mycel In SearchRange
    If Mycel.Text >= LB And Mycel.Text <= UB Then
    n = n + Criteria_Range.Cells(Mycel.Row, 1).Text
    i = i + 1
    End If
    Next
            
    AverageBetween = n / i
    
    End Function
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,134
Members
452,890
Latest member
Nikhil Ramesh

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