Thread: Please Help
View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Frank Kabel Frank Kabel is offline
external usenet poster
 
Posts: 3,885
Default Please Help

Hi
a small correction: change the line
ret_str = CInt(ret_value * 24) & "H:" & (ret_value * 24 -
CInt(ret_value * 24)) * 60 & "M"

to
ret_str = CInt(ret_value * 24) & "H:" & CInt((ret_value * 24 - _
CInt(ret_value * 24)) * 60) & "M"


--
Regards
Frank Kabel
Frankfurt, Germany

Frank Kabel wrote:
Hi
you may try the following user defined function:

-----
Function sum_pseudo_time(rng As Range, Optional color_index As
Integer) As String
Dim cell As Range
Dim ret_str As String
Dim ret_value
Dim time_str As String

ret_value = 0
For Each cell In rng
If color_index Then
If cell.Value < "" And cell.Interior.ColorIndex =

color_index
Then
time_str = Replace(Replace(cell.Value, "H", ""), "M", "")
ret_value = ret_value + CDate(time_str)
End If
Else
If cell.Value < "" Then
time_str = Replace(Replace(cell.Value, "H", ""), "M", "")
ret_value = ret_value + CDate(time_str)
End If
End If
Next
ret_str = CInt(ret_value * 24) & "H:" & (ret_value * 24 -
CInt(ret_value * 24)) * 60 & "M"

sum_pseudo_time = ret_str
End Function

-----

Call this function either without a color_index parameter:
=sum_pseudo_time(A1:A10)
this will return the sum of your cells regardless of their color

or use the color_index parameter (the functions used the color of the
interior/not the font color):
=sum_pseudo_time(A11:A13;3)
this will sum all cells this the color_index = 3 (this is red)
A table of color_indexes can be found on:
http://www.mvps.org/dmcritchie/excel/colors.htm





I have the following situation:

Column D
0H:8M
9H:39M
14H:46M ... and so on.

these cells DO NOT have any format. I wish to add the values before
the "H" and the values before the "M" and still have them separated
by the ":".

for example...
Column D
0H:8M
9H:39M
----------
Total
9H:47M

To complicate things even more,... there are 3 different color cells
within column D. Below you'll see each color property (I don't know
if this could help).

Color.........R.....G.....B
Green.....204...255..204
Yellow....255...255..153
Red........255...128..128

What I want to accomplish is the following:
Select a range in column D of... let's say 200 rows and have code...
using something like an input box add total time for Green, Yellow
and Red cells... Therefore, adding 200 rows for example might yield

a
total green cells 450H:46M.

The following could be a beginning... for selecting range using an
input box.

'...Thanks Tom O
On error resume next
set rng = Application.InputBox( _
"Please select range with mouse", type:=8)
On Error goto 0
if not rng is nothing then

Also, I Found this on Chip Pearson's site...

Summing The Values Of Cells With A Specific Color

The following function will return the sum of cells in a range that
have either an Interior (background) or Font of a specified color.
InRange is the range of cells to examine, WhatColorIndex is the
ColorIndex value to count, and OfText indicates whether to return

the
ColorIndex of the Font (if True) or the Interior (if False).
Function SumByColor(InRange As Range, WhatColorIndex As Integer, _
Optional OfText As Boolean = False) As Double
'
' This function return the SUM of the values of cells in
' InRange with a background color, or if OfText is True a
' font color, equal to WhatColorIndex.
'
Dim Rng As Range
Dim OK As Boolean

Application.Volatile True
For Each Rng In InRange.Cells
If OfText = True Then
OK = (Rng.Font.ColorIndex = WhatColorIndex)
Else
OK = (Rng.Interior.ColorIndex = WhatColorIndex)
End If
If OK And IsNumeric(Rng.Value) Then
SumByColor = SumByColor + Rng.Value
End If
Next Rng

End Function
You can call this function from a worksheet cell with a formula like
=SUMBYCOLOR(A1:A10,3,FALSE)


How do I integrate these codes together? can any1 help??
I realize that this is a daunting task.... but I would really be
gratefull beyong belief, if anyone can come up with a solution.

If any1 wishes... I coud email the workbook as well.


Larry
VBA Amateur


---
Message posted from http://www.ExcelForum.com/