Loop Code?

D

documike

I have a macro that when run changes the color of a row based on a cell
value.
Col A has TRUE & FALSE values and the color of the row changes whether TRUE
or FALSE.
It works fine when I manually run the macro.
I want it to dynamically change whenever one of the cells in Col A changes.
Is this when a LOOP is required? How do I code that? Here is the code I am
currently playing with:

Sub ColorRowBasedOnCellValue()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim cell As Range
For Each cell In Intersect(Selection, ActiveCell.EntireColumn, _
ActiveSheet.UsedRange)
Select Case cell.Value
Case Is >= TRUE
cell.EntireRow.Interior.colorindex = 20
Case Is >= FALSE
cell.EntireRow.Interior.colorindex = 37
End Select
Next cell
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = False
End Sub
 
D

Dave Peterson

Instead of using code, you may want to look at:
Format|conditional formatting

But if you have to use code, how are the cells changing.

If it's by typing, you could use a worksheet_change event and just check the
cells that change.
 
E

Earl Kiosterud

Mike,

I'm not sure why your using >= TRUE. Normally, you'd use = TRUE or = FALSE.

Your routine does loop through the cells, via the For Each cell in .... To
get it to change automatically, use an event procedure. Looping isn't
required:

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A:A")) Is Nothing Then ' are we in column
A?
Select Case Target.Value
Case Is = True
Target.EntireRow.Interior.ColorIndex = 20
Case Is = False
Target.EntireRow.Interior.ColorIndex = 37
End Select

End If
End Sub

Put this in the sheet module, not a regular module.
 
D

Dana DeLouis

Just a general idea. If the cells only have True/False, perhaps another
option...

For Each cell In [Your Range Here].Cells
cell.EntireRow.Interior.ColorIndex = 37 - 17 * cell.Value
Next

Another option might be to use SpecialCells

Columns("A:A").SpecialCells(xlCellTypeConstants, xlLogical)...etc
 
H

Harald Staff

Hi Dana

Nice idea, but note that TRUE is -1 in VB / VBA.

Best wishes Harald

Dana DeLouis said:
Just a general idea. If the cells only have True/False, perhaps another
option...

For Each cell In [Your Range Here].Cells
cell.EntireRow.Interior.ColorIndex = 37 - 17 * cell.Value
Next

Another option might be to use SpecialCells

Columns("A:A").SpecialCells(xlCellTypeConstants, xlLogical)...etc

--
Dana DeLouis
Win XP & Office 2003


documike said:
I have a macro that when run changes the color of a row based on a cell
value.
Col A has TRUE & FALSE values and the color of the row changes whether
TRUE or FALSE.
It works fine when I manually run the macro.
I want it to dynamically change whenever one of the cells in Col A
changes. Is this when a LOOP is required? How do I code that? Here is
the code I am currently playing with:

Sub ColorRowBasedOnCellValue()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim cell As Range
For Each cell In Intersect(Selection, ActiveCell.EntireColumn, _
ActiveSheet.UsedRange)
Select Case cell.Value
Case Is >= TRUE
cell.EntireRow.Interior.colorindex = 20
Case Is >= FALSE
cell.EntireRow.Interior.colorindex = 37
End Select
Next cell
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = False
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top