I hopesomeone can edit this fuction so i can use it including an if criteria

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,194
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi Everyone,
I have this function i use a lot,

VBA Code:
Function LogAverage(ParamArray AveCells() As Variant) As Double
   
    Dim out As Double
    Dim cel As Range
    Dim count, i As Long
    
    count = 0
    out = 0

    For i = LBound(AveCells) To UBound(AveCells)
    
        If TypeName(AveCells(i)) = "Range" Then
            
            For Each cel In AveCells(i)
                
                If IsEmpty(cel) Then
                    'skip blank cells
                Else
                    out = out + 10 ^ (cel.Value / 10)
                    count = count + 1
               End If
               
            Next cel
            
        Else
        
            If IsEmpty(AveCells(i)) Then
                'skip blank cells
            Else
                out = out + 10 ^ (AveCells(i) / 10)
                count = count + 1
            End If
            
        End If
    
    Next i

    LogAverage = 10 * Log(out / count) / Log(10)

End Function

which allows me to get a log average over a range,
I'm woundering if it would be possible to change it so it gives the log average over the range of cells that match the cell next to it, (Like sumif and countif do)

so instead of just
=LogAverage(C26:C500)
Maybe
in C2 it would read

=LogAverageIf(A26:A500,A2,C26:C500) (With A2 being the critiria and A26:A500, the criteria range)

please help if you can
Thanks
Tony
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Maybe something like this:

VBA Code:
Function LogAverageIf(CriteriaRange As Range, Criteria As Variant, ParamArray AveCells() As Variant) As Double

    Dim out As Double
    Dim cel As Range
    Dim count, i As Long
    
    count = 0
    out = 0

    For i = LBound(AveCells) To UBound(AveCells)
    
        If TypeName(AveCells(i)) = "Range" Then
            
            For Each cel In AveCells(i)
                
                If IsEmpty(cel) Then
                    'skip blank cells
                Else
                    If cel.Offset(0, -1).Value = Criteria Then
                        out = out + 10 ^ (cel.Value / 10)
                        count = count + 1
                    End If
               End If
               
            Next cel
            
        Else
        
            If IsEmpty(AveCells(i)) Then
                'skip blank cells
            Else
                out = out + 10 ^ (AveCells(i) / 10)
                count = count + 1
            End If
            
        End If
    
    Next i

    LogAverageIf = 10 * Log(out / count) / Log(10)

End Function

This new function takes in two additional parameters: CriteriaRange and Criteria. CriteriaRange is the range of cells that contains the criteria, and Criteria is the actual criteria you want to match against.

The function loops through each cell in the AveCells range and checks if the value in the cell next to it (offset by one column to the left) matches the Criteria. If it does, then it includes that cell in the calculation of the log average.

You can use this function just like your example by entering =LogAverageIf(A26:A500,A2,C26:C500) in cell C2.
 
Upvote 0

Forum statistics

Threads
1,214,805
Messages
6,121,665
Members
449,045
Latest member
Marcus05

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