Function works on msgbox but not in cell formula

lipanook

New Member
Joined
Nov 29, 2012
Messages
23
I've written a function to calculate the geometric mean of a set of data that fit certain criteria (similar to the native averageif() or sumif() functions). It works if I use a test MsgBox, but I can't get it to work as a formula in a cell. The formula I've written is:

=geomeanif(M:M,A:A,V2)

The result is #VALUE !

If I try it in a debugging subroutine, it works well:
Code:
[INDENT][FONT=arial][FONT=courier new][/FONT][B][I][U][SUB][SUP]<strike>
</strike>[/SUP][/SUB][/U][/I][/B][FONT=courier new]Sub test()
    MsgBox geomeanif(Range("m:m"), Range("a:a"), "Test Criteria")
End Sub<strike></strike>[/FONT]
[/FONT][/INDENT]

This gives me a sensible double as a result. I can't figure out why it works in the MsgBox but not the cell formula. What am I missing here?

Here is my function:

Code:
[INDENT][FONT=courier new]Public Function geomeanif(geomeanRange As Range, criteriaRange As Range, criteria)[/FONT][/INDENT]
[INDENT][FONT=courier new]    FirstRow = geomeanRange.Cells(1, 1).Row 'Set first row of data range
    LastRow = geomeanRange.Cells.SpecialCells(xlCellTypeLastCell).Row 'Set last row of data range
    
    LogSum = 0
    dataCount = 0
    
    'Loop through rows in the data range. If the value in the criteriaRange column is the same as the criteria, then add the log10 value of the geomeanRange column cell to the cumulative sum.
    For i = 1 To LastRow
        If criteriaRange.Cells(i, 1).Value = criteria Then
            LogSum = LogSum + Log10(geomeanRange.Cells(i, 1).Value)
            dataCount = dataCount + 1 'count the number of data that match the criteria
        End If
    
    Next i
    
    'Calculate geomean
    geomeanif = 10 ^ (LogSum / dataCount)[/FONT][/INDENT]
[INDENT][FONT=courier new]End Function[/FONT][/INDENT]
<strike>
</strike>


 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Try replacing this line
Code:
    lastRow = geomeanRange.Cells.SpecialCells(xlCellTypeLastCell).Row    'Set last row of data range
with this one
Code:
    lastRow = Cells(Rows.Count, geomeanRange.Column).End(xlUp).Row  'Set last row of data range
 
Upvote 0

Forum statistics

Threads
1,214,957
Messages
6,122,472
Members
449,087
Latest member
RExcelSearch

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