CF or VBA

J

Jock

In a sheet with (max)10000 rows, any of the rows could be shaded green(when
criteria 1 is met), orange (criteria 2), or red (3).
The criteria will appear in column "AB" by formula e.g. =IF($S4>10,"W","").
This formula will change as I add bits to it but the premise is that the code
needs to look at column AB and deal with whatever result is displayed by the
formula (max of 3.
If the criteria in any cell in "AB" alters or is removed entirely, the row
will need to reflect this.
How would I do this by code?
 
G

Gary''s Student

In this example the formulae in column AB will return "G" or "O" or "R" for
green or orange or red. It is event code to be inserted in the worksheet
code area:

Private Sub Worksheet_Calculate()
Dim n As Long
n = Cells(Rows.Count, "AB").End(xlUp).Row
For i = 1 To n
codee = Cells(i, "AB").Value
Select Case codee
Case "G"
clr = 10
Case "O"
clr = 46
Case "R"
clr = 3
Case Else
clr = xlNone
End Select
With Cells(i, "AB").EntireRow
.Interior.ColorIndex = clr
End With
Next
End Sub


Because it is worksheet code, it is very easy to install and use:

1. right-click the tab name near the bottom of the window
2. select View Code - this brings up a VBE window
3. paste the stuff in and close the VBE window

If you save the workbook, the macro will be saved with it.

To remove the macro:

1. bring up the VBE windows as above
2. clear the code out
3. close the VBE window

To learn more about macros in general, see:

http://www.mvps.org/dmcritchie/excel/getstarted.htm

To learn more about Event Macros (worksheet code), see:

http://www.mvps.org/dmcritchie/excel/event.htm
 
J

Jock

Thanks.
Can you make two changes?
1) Columns in the sheet are already formatted with colour to visually group
certain ones together. Your code strips all colour from all rows from the
active cell in AB up unless one of the options is selected in the active cell
in which case the relevant colour is applied to the active row.
2) When the option is deleted from the active cell in AB, the vba applied
colour remains on the active row.

Can the code:
1) ignore all row formatting except for the active row, and
2) when an option is removed from AB, return the row to its original
formatting (the same as row 3 for instance)?

Thanks again
 

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