Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 6
Default Average cells based on column header

On Apr 6, 12:24*pm, GS wrote:
Here's a quick-n-dirty function that accepts 6 different operators for
specifying criteria. (watch out for word wrap)

Public Function Average_2Ifs(RangeToAvg As Range, _
* * * * * * * * Criteria1Range As Range, Criteria1 As Variant, _
* * * * * * * * Criteria2Range As Range, Criteria2 As Variant) As
Double
' Returns the average of a range of values based on 2 specified
criteria.
' Criteria can be the same range or different ranges.

* Dim sz As String, c As Range
* Dim dValues As Double, iCount As Integer
* Dim v1 As Variant, v2 As Variant

* 'Check combined operators first
* If InStr(1, Criteria2, "<=", vbTextCompare) 0 Then sz = "<=": GoTo
GotIt
* If InStr(1, Criteria2, "=", vbTextCompare) 0 Then sz = "=": GoTo
GotIt
* If InStr(1, Criteria2, "<", vbTextCompare) 0 Then sz = "<": GoTo
GotIt
* 'If we got here then single operator used
* If InStr(1, Criteria2, "<", vbTextCompare) 0 Then sz = "<": GoTo
GotIt
* If InStr(1, Criteria2, "", vbTextCompare) 0 Then sz = "": GoTo
GotIt
* If InStr(1, Criteria2, "=", vbTextCompare) 0 Then sz = "=": GoTo
GotIt

GotIt:
* v2 = CDbl(Mid(Criteria2, Len(sz) + 1))
* For Each c In Criteria1Range
* * If c.Value = Criteria1 Then
* * * v1 = Cells(Criteria2Range.Row, c.Column).Value
* * * Select Case sz
* * * * 'Check combined operators first
* * * * Case "<=": If v1 <= v2 Then dValues = dValues + v1: iCount =
iCount + 1
* * * * Case "=": If v1 = v2 Then dValues = dValues + v1: iCount =
iCount + 1
* * * * Case "<": If v1 < v2 Then dValues = dValues + v1: iCount =
iCount + 1
* * * * 'If we got here then single operator used
* * * * Case "<": If v1 < v2 Then dValues = dValues + v1: iCount =
iCount + 1
* * * * Case "": If v1 v2 Then dValues = dValues + v1: iCount =
iCount + 1
* * * * Case "=": If v1 = v2 Then dValues = dValues + v1: iCount =
iCount + 1
* * * End Select
* * End If
* Next
* Average_2Ifs = (dValues / iCount)
End Function 'Average_2Ifs()

Example usage:
* Formula to put in the target cell:

* * =average_2ifs($F$5:$HB$5,$F$1:$HB$1,"SCORE",$F$5:$ HB$5,"0")

* * **Assumes each 'SCORE' column is labeled "SCORE" in Row1.

I'm also working on a worksheet formula solution which I'll post later.

--
Garry

Free usenet access athttp://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc


Thank you, Garry. I'll give this a try.
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Return column header based on last value in row C. Excel Worksheet Functions 3 April 12th 10 08:53 AM
Returning Column Header based on Row and Value Chad DiGregorio New Users to Excel 3 July 6th 09 07:09 PM
column data retrieved based on value in header row Tomkat743 Excel Discussion (Misc queries) 2 March 4th 09 04:17 AM
pick from table based on row and column header PBcorn Excel Worksheet Functions 2 June 29th 08 07:00 PM
calculate average in a column based on criteria in another column sharon t Excel Discussion (Misc queries) 2 May 12th 06 06:07 PM


All times are GMT +1. The time now is 12:08 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"