VBA Example - Change Font Color

Suppose you have a table of students scores for the math exam. Also assume the maximum possible score is 100. Therefore, any value above 100 is an error. And you want to highlight the error values in red. See the data below.


The VBA code is this:

'If a score>100, it is an error. Mark it in red.
Sub Change_color()
    Dim row As Integer
    Dim score As Integer
    For row = 2 To 16
        score = Cells(row, 2)
        If score > 100 Then
            Cells(row, 2).Font.Color = RGB(255, 0, 0)
        End If
    Next
    MsgBox ("done")
End Sub

The RGB() function is the function that Excel uses to set colors. RGB(255, 0, 0) is for red.

By running the code, you get the maximum value:

Comments

Popular posts from this blog

Excel - IF function

Excel - Hierarchical chart (Treemap)

Excel - VLOOKUP function