Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default Iterate Thru Cells

Hi all

Probably a basic one, but I've been away from this for a bit & I've
forgotten how to do this...

Trying to write a function that iterates thru the cells in a range, and
comes to a total depending on the BackColor of each cell.

Calling it like this, which results in #Value! -
=ReturnPercentage(E8:AB8)

Function ReturnPercentage(InRange As Range) As Integer
'Iterates thru cells in inRange, providing possible max total of cells
unless cell has BackColor of Black or is blank,
'in which case 3 is deducted from poss max total

Application.Volatile True

Dim PossMax As Integer

Dim tmp

For Each cell In ActiveSheet.Range(InRange)
tmp = InRange(1, 1).Interior.ColorIndex

Select Case tmp
Case 56, -4142 'Black or null/white
PossMax = PossMax - 3
Case Else
PossMax = PossMax + 3
End Select
Next cell

ReturnPercentage = PossMax

End Function

.... All/any help gratefully received.

Thanks for looking

Kelvin
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default Iterate Thru Cells

Hi,

I'm not sure what your trying to do here because this returns either -3 or
+3 for each cell in the range but it now will iterate through all cells in
your range.

Function ReturnPercentage(InRange As Range) As Integer
'Iterates thru cells in inRange, providing possible max total of cells
'unless cell has BackColor of Black or is blank,
'in which case 3 is deducted from poss max total
Application.Volatile True
Dim PossMax As Integer
Dim tmp
For Each c In InRange
tmp = c.Interior.ColorIndex
Select Case tmp
Case 56, -4142 'Black or null/white
PossMax = PossMax - 3
Case Else
PossMax = PossMax + 3
End Select
Next c
ReturnPercentage = PossMax
End Function

Mike

"Kelvin" wrote:

Hi all

Probably a basic one, but I've been away from this for a bit & I've
forgotten how to do this...

Trying to write a function that iterates thru the cells in a range, and
comes to a total depending on the BackColor of each cell.

Calling it like this, which results in #Value! -
=ReturnPercentage(E8:AB8)

Function ReturnPercentage(InRange As Range) As Integer
'Iterates thru cells in inRange, providing possible max total of cells
unless cell has BackColor of Black or is blank,
'in which case 3 is deducted from poss max total

Application.Volatile True

Dim PossMax As Integer

Dim tmp

For Each cell In ActiveSheet.Range(InRange)
tmp = InRange(1, 1).Interior.ColorIndex

Select Case tmp
Case 56, -4142 'Black or null/white
PossMax = PossMax - 3
Case Else
PossMax = PossMax + 3
End Select
Next cell

ReturnPercentage = PossMax

End Function

... All/any help gratefully received.

Thanks for looking

Kelvin

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default Iterate Thru Cells

Thanks Mike - your answer's nailed it, as ever!

Incidentally, the idea here is iterate thru a range of results that are
coloured red/amber/green for various units, but if a result is n/a for a unit
then the cell's coloured black/not colored and the total is adjusted
downwards accordingly for that unit. Which is what they want..!

Regards,

KC

"Mike H" wrote:

Hi,

I'm not sure what your trying to do here because this returns either -3 or
+3 for each cell in the range but it now will iterate through all cells in
your range.

Function ReturnPercentage(InRange As Range) As Integer
'Iterates thru cells in inRange, providing possible max total of cells
'unless cell has BackColor of Black or is blank,
'in which case 3 is deducted from poss max total
Application.Volatile True
Dim PossMax As Integer
Dim tmp
For Each c In InRange
tmp = c.Interior.ColorIndex
Select Case tmp
Case 56, -4142 'Black or null/white
PossMax = PossMax - 3
Case Else
PossMax = PossMax + 3
End Select
Next c
ReturnPercentage = PossMax
End Function

Mike

"Kelvin" wrote:

Hi all

Probably a basic one, but I've been away from this for a bit & I've
forgotten how to do this...

Trying to write a function that iterates thru the cells in a range, and
comes to a total depending on the BackColor of each cell.

Calling it like this, which results in #Value! -
=ReturnPercentage(E8:AB8)

Function ReturnPercentage(InRange As Range) As Integer
'Iterates thru cells in inRange, providing possible max total of cells
unless cell has BackColor of Black or is blank,
'in which case 3 is deducted from poss max total

Application.Volatile True

Dim PossMax As Integer

Dim tmp

For Each cell In ActiveSheet.Range(InRange)
tmp = InRange(1, 1).Interior.ColorIndex

Select Case tmp
Case 56, -4142 'Black or null/white
PossMax = PossMax - 3
Case Else
PossMax = PossMax + 3
End Select
Next cell

ReturnPercentage = PossMax

End Function

... All/any help gratefully received.

Thanks for looking

Kelvin

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default Iterate Thru Cells

Well if that's what the Customer wants then so be it. Thanks for the feedback.

Mike

"Kelvin" wrote:

Thanks Mike - your answer's nailed it, as ever!

Incidentally, the idea here is iterate thru a range of results that are
coloured red/amber/green for various units, but if a result is n/a for a unit
then the cell's coloured black/not colored and the total is adjusted
downwards accordingly for that unit. Which is what they want..!

Regards,

KC

"Mike H" wrote:

Hi,

I'm not sure what your trying to do here because this returns either -3 or
+3 for each cell in the range but it now will iterate through all cells in
your range.

Function ReturnPercentage(InRange As Range) As Integer
'Iterates thru cells in inRange, providing possible max total of cells
'unless cell has BackColor of Black or is blank,
'in which case 3 is deducted from poss max total
Application.Volatile True
Dim PossMax As Integer
Dim tmp
For Each c In InRange
tmp = c.Interior.ColorIndex
Select Case tmp
Case 56, -4142 'Black or null/white
PossMax = PossMax - 3
Case Else
PossMax = PossMax + 3
End Select
Next c
ReturnPercentage = PossMax
End Function

Mike

"Kelvin" wrote:

Hi all

Probably a basic one, but I've been away from this for a bit & I've
forgotten how to do this...

Trying to write a function that iterates thru the cells in a range, and
comes to a total depending on the BackColor of each cell.

Calling it like this, which results in #Value! -
=ReturnPercentage(E8:AB8)

Function ReturnPercentage(InRange As Range) As Integer
'Iterates thru cells in inRange, providing possible max total of cells
unless cell has BackColor of Black or is blank,
'in which case 3 is deducted from poss max total

Application.Volatile True

Dim PossMax As Integer

Dim tmp

For Each cell In ActiveSheet.Range(InRange)
tmp = InRange(1, 1).Interior.ColorIndex

Select Case tmp
Case 56, -4142 'Black or null/white
PossMax = PossMax - 3
Case Else
PossMax = PossMax + 3
End Select
Next cell

ReturnPercentage = PossMax

End Function

... All/any help gratefully received.

Thanks for looking

Kelvin

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,202
Default Iterate Thru Cells

Not sure what the +3/-3 is all about, but given what you posted, I think
this will do what you want...

Function ReturnPercentage(InRange As Range) As Integer
Dim Cell As Range
Application.Volatile True
For Each Cell In InRange
Select Case Cell.Interior.ColorIndex
Case 56, -4142 'Black or null/white
ReturnPercentage = ReturnPercentage - 3
Case Else
ReturnPercentage = ReturnPercentage + 3
End Select
Next
End Function

Rick


"Kelvin" wrote in message
...
Hi all

Probably a basic one, but I've been away from this for a bit & I've
forgotten how to do this...

Trying to write a function that iterates thru the cells in a range, and
comes to a total depending on the BackColor of each cell.

Calling it like this, which results in #Value! -
=ReturnPercentage(E8:AB8)

Function ReturnPercentage(InRange As Range) As Integer
'Iterates thru cells in inRange, providing possible max total of cells
unless cell has BackColor of Black or is blank,
'in which case 3 is deducted from poss max total

Application.Volatile True

Dim PossMax As Integer

Dim tmp

For Each cell In ActiveSheet.Range(InRange)
tmp = InRange(1, 1).Interior.ColorIndex

Select Case tmp
Case 56, -4142 'Black or null/white
PossMax = PossMax - 3
Case Else
PossMax = PossMax + 3
End Select
Next cell

ReturnPercentage = PossMax

End Function

... All/any help gratefully received.

Thanks for looking

Kelvin




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
Use "For... Each" to Iterate Through a Cells Collection? BobbyMurcerFan Excel Programming 1 March 18th 06 07:27 PM
iterate columns to select certain cells Dale[_16_] Excel Programming 4 February 24th 06 05:48 PM
Set MyRange for non-adjacent cells and iterate through? Ed Excel Programming 2 January 3rd 06 06:13 PM
Can't iterate thru a row with For peter Excel Programming 2 January 7th 05 04:46 AM
How To Iterate a range of cells? StarGazerNC Excel Programming 5 August 4th 04 04:51 PM


All times are GMT +1. The time now is 01:11 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"