conditional formatting macro


Posted by Steve on December 13, 2001 4:54 AM

Can I apply conditional formatting from a macro - and then dissaply it? I want to scan a range of marks and at the click of a button run a macro that highlights all those with less than 3, then I want to clear the formatting again.



Posted by Petruchio on December 13, 2001 5:23 AM


Assuming the marks are in Column A, try this :-

Sub Highlight()
Dim rng As Range, grades As Range, cell As Range
Set grades = Range("A:A") 'change column ref as required
Set rng = Intersect(grades, ActiveSheet.UsedRange)
For Each cell In rng
If cell.Value < 3 And cell.Value <> "" Then
cell.Interior.ColorIndex = 6
Else: cell.Interior.ColorIndex = xlNone
End If
Next
End Sub

Sub Remove_Highlight()
Range("A:A").Interior.ColorIndex = xlNone
End Sub