0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Q10.セルの文字色を変えるには?

Posted at

今回は、セルの文字色について考えていきます。
以下の画像をご覧ください。

Q9.png

前回のQ9の結果の画像です。
今回はこちらの画像を加工していきましょう。

どのように加工していくかというと、セルの文字色は変えます。
80点以上を青色に、30点未満を赤点に変えるマクロを作ってみましょう。

■ポイント

ポイントはもちろんセルの文字色をどうやって変えるかというところ。
セルの文字色はRangeオブジェクトのFontプロパティのColorIndexプロパティを使うことが変えることが
できます。
ただし、ColorIndexプロパティの番号に対応する色を事前に調べておかなければいけません。

ちなみに今回使用する青色は5で赤色は3となります。

■回答

Sub Q10_Answer()
    Dim r As Range
    For Each r In Range("C3:E15")
        If r.Value > 79 Then
            r.Font.ColorIndex = 5
        End If
        If r.Value < 30 Then
            r.Font.ColorIndex = 3
        End If
    Next r
End Sub

■解説

今回は、ForEach文を使用して、データが記入されているセルを
ダイレクトに指定しております。
該当セル範囲内の値をひとつひとつ見ていき、
If文で条件分岐しています。

r.Font.ColorIndex = 5

上記のように記載することで、セルの文字色を変えることができます。
この場合は青色になります。

最後に実行結果はこちらになります。

Q10.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?