#1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 410
Default Average by color VBA

I have read many posts on how to Count cells with fill color and how
to sum based on fill color but how do you do an average? Here is the
code I have now.

Function AVGColor(rColor As Range, Range As Range)
Dim rCell
Dim iCol As Integer
Dim C As Range
Dim vResult As Double

iCol = rColor.Interior.ColorIndex
For Each rCell In Range
If rCell.Interior.ColorIndex = iCol Then
vResult = WorksheetFunction.Sum(rCell, vResult)
End If
Next rCell

For Each C In Range
If C.Interior.ColorIndex = rColor.Interior.ColorIndex Then
Count = Count + 1
End If
Next C

If AVGColor = vResult / Count = Numeric Then
AVGColor = vResult / Count
Else: AVGColor = ""
End If

End Function
Thanks for the help,
Jay

  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 10,593
Default Average by color VBA

Function AVGColor(rColor As Range, rng As Range)
Dim iCol As Integer
Dim C As Range
Dim vResult As Double
Dim Count As Long

iCol = rColor.Interior.ColorIndex
For Each C In rng
If C.Interior.ColorIndex = iCol Then
vResult = WorksheetFunction.Sum(C, vResult)
Count = Count + 1
End If
Next rCell

If Count = 0 Then
AVGColor = ""
Else
AVGColor = vResult / Count
End If

End Function

--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

"jlclyde" wrote in message
oups.com...
I have read many posts on how to Count cells with fill color and how
to sum based on fill color but how do you do an average? Here is the
code I have now.

Function AVGColor(rColor As Range, Range As Range)
Dim rCell
Dim iCol As Integer
Dim C As Range
Dim vResult As Double

iCol = rColor.Interior.ColorIndex
For Each rCell In Range
If rCell.Interior.ColorIndex = iCol Then
vResult = WorksheetFunction.Sum(rCell, vResult)
End If
Next rCell

For Each C In Range
If C.Interior.ColorIndex = rColor.Interior.ColorIndex Then
Count = Count + 1
End If
Next C

If AVGColor = vResult / Count = Numeric Then
AVGColor = vResult / Count
Else: AVGColor = ""
End If

End Function
Thanks for the help,
Jay



  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 10,593
Default Average by color VBA

should be

Function AVGColor(rColor As Range, rng As Range)
Dim iCol As Integer
Dim C As Range
Dim vResult As Double
Dim Count As Long

iCol = rColor.Interior.ColorIndex
For Each C In rng
If C.Interior.ColorIndex = iCol Then
vResult = vResult + C.Value
Count = Count + 1
End If
Next rCell

If Count = 0 Then
AVGColor = ""
Else
AVGColor = vResult / Count
End If

End Function

--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

"jlclyde" wrote in message
oups.com...
I have read many posts on how to Count cells with fill color and how
to sum based on fill color but how do you do an average? Here is the
code I have now.

Function AVGColor(rColor As Range, Range As Range)
Dim rCell
Dim iCol As Integer
Dim C As Range
Dim vResult As Double

iCol = rColor.Interior.ColorIndex
For Each rCell In Range
If rCell.Interior.ColorIndex = iCol Then
vResult = WorksheetFunction.Sum(rCell, vResult)
End If
Next rCell

For Each C In Range
If C.Interior.ColorIndex = rColor.Interior.ColorIndex Then
Count = Count + 1
End If
Next C

If AVGColor = vResult / Count = Numeric Then
AVGColor = vResult / Count
Else: AVGColor = ""
End If

End Function
Thanks for the help,
Jay



  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 410
Default Average by color VBA

On Aug 30, 12:49 pm, "Bob Phillips" wrote:
should be

Function AVGColor(rColor As Range, rng As Range)
Dim iCol As Integer
Dim C As Range
Dim vResult As Double
Dim Count As Long

iCol = rColor.Interior.ColorIndex
For Each C In rng
If C.Interior.ColorIndex = iCol Then
vResult = vResult + C.Value
Count = Count + 1
End If
Next rCell

If Count = 0 Then
AVGColor = ""
Else
AVGColor = vResult / Count
End If

End Function

--
HTH

Bob

The formula needs to skip blank cells liek the Average formula in
Excel. This Function does what mine does, just more efficiently.
Jay

  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 10,593
Default Average by color VBA

Wouldn't the blanks not be coloured?

Function AVGColor(rColor As Range, rng As Range)
Dim iCol As Integer
Dim C As Range
Dim vResult As Double
Dim Count As Long

iCol = rColor.Interior.ColorIndex
For Each C In rng
If C.Interior.ColorIndex = iCol And IsNumeric(C.Value) Then
vResult = vResult + C.Value
Count = Count + 1
End If
Next rCell

If Count = 0 Then
AVGColor = ""
Else
AVGColor = vResult / Count
End If

End Function


--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

"jlclyde" wrote in message
oups.com...
On Aug 30, 12:49 pm, "Bob Phillips" wrote:
should be

Function AVGColor(rColor As Range, rng As Range)
Dim iCol As Integer
Dim C As Range
Dim vResult As Double
Dim Count As Long

iCol = rColor.Interior.ColorIndex
For Each C In rng
If C.Interior.ColorIndex = iCol Then
vResult = vResult + C.Value
Count = Count + 1
End If
Next rCell

If Count = 0 Then
AVGColor = ""
Else
AVGColor = vResult / Count
End If

End Function

--
HTH

Bob

The formula needs to skip blank cells liek the Average formula in
Excel. This Function does what mine does, just more efficiently.
Jay





  #6   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 410
Default Average by color VBA

On Aug 30, 1:13 pm, "Bob Phillips" wrote:
Wouldn't the blanks not be coloured?

Function AVGColor(rColor As Range, rng As Range)
Dim iCol As Integer
Dim C As Range
Dim vResult As Double
Dim Count As Long

iCol = rColor.Interior.ColorIndex
For Each C In rng
If C.Interior.ColorIndex = iCol And IsNumeric(C.Value) Then
vResult = vResult + C.Value
Count = Count + 1
End If
Next rCell

If Count = 0 Then
AVGColor = ""
Else
AVGColor = vResult / Count
End If

End Function

--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

"jlclyde" wrote in message

oups.com...



On Aug 30, 12:49 pm, "Bob Phillips" wrote:
should be


Function AVGColor(rColor As Range, rng As Range)
Dim iCol As Integer
Dim C As Range
Dim vResult As Double
Dim Count As Long


iCol = rColor.Interior.ColorIndex
For Each C In rng
If C.Interior.ColorIndex = iCol Then
vResult = vResult + C.Value
Count = Count + 1
End If
Next rCell


If Count = 0 Then
AVGColor = ""
Else
AVGColor = vResult / Count
End If


End Function


--
HTH


Bob

The formula needs to skip blank cells liek the Average formula in
Excel. This Function does what mine does, just more efficiently.
Jay- Hide quoted text -


- Show quoted text -


This is what the code shoudl be. Thank you for turing me onto the
right answer.
Jay
If C.Interior.ColorIndex = iCol And IsNumeric(C.Value) And Not
IsEmpty(C.Value) Then

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
Find monthly average but have average automatically configured kimbafred Excel Discussion (Misc queries) 2 August 8th 07 12:28 AM
average cells, show 0 if nothing to average Kycajun Excel Discussion (Misc queries) 8 June 21st 06 07:36 PM
Error Handling #N/A with AVERAGE Function - Average of values in Row Sam via OfficeKB.com Excel Worksheet Functions 13 July 31st 05 03:59 PM
Weighed Average of a weiged average when there are blanks krl - ExcelForums.com Excel Discussion (Misc queries) 1 July 6th 05 07:37 PM
how does one convert text to a formula "average(A:A)" to =average( phshirk Excel Worksheet Functions 4 April 14th 05 01:20 AM


All times are GMT +1. The time now is 04:00 PM.

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

About Us

"It's about Microsoft Excel"