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?

【EXCEL】色のついたセルの数え方

Last updated at Posted at 2025-04-22

やること

 EXCELで、色のついたセルの数え方をメモしておく。

事前確認:Qiitaでの記事

 ということで、いきなりですが以下の検索をしたところ、2件ヒットした。(2025/04/22時点)
「DisplayFormat.Interior.ColorIndex」の検索結果 - Qiita
 この2個を紹介。

 ちなみに、以下の検索をしたところ、121件ヒットした。(2025/04/22時点)
「Interior.ColorIndex」の検索結果 - Qiita
 ここからいくつか紹介。

チートシート

【永久保存版】Excel VBA Color 変更 チートシート CRUD 「色付け・色読み取り・リセット」 #crud - Qiita
最終更新日 2023年07月19日 投稿日 2019年06月10日
 「塗りつぶし色」、「フォントの色」、「図形の塗りつぶし色」、「図形の枠線色」、「RBGとColorIndexなど」のまとめ。

リファレンス

Excel VBA リファレンス 項目リスト #list - Qiita
最終更新日 2019年09月22日 投稿日 2018年10月20日
 英語版からExcelのオブジェクト項目を採取したもの。

Interior.ColorIndexの検索結果から

【VBA】クリックしたセルの色を変える方法 #Excel - Qiita
最終更新日 2021年04月09日 投稿日 2021年04月09日
VBA セル着色 まとめ #Excel - Qiita
最終更新日 2023年04月17日 投稿日 2023年04月17日
【VBA】セルの塗りつぶしと解除 #VBA - Qiita
最終更新日 2021年04月20日 投稿日 2021年04月19日
Excel Word PowerPoint VBA 56色カラーパレットの色をRGB分解してリスト化する #Publisher - Qiita
最終更新日 2019年09月22日 投稿日 2019年05月19日
Excel VBA(マクロ)で一部の文字色を変更するときはフォントを揃えたほうがいい #ExcelVBA - Qiita
最終更新日 2022年05月06日 投稿日 2022年05月06日
【ExcelVBA】爆速カラーリングの戦術メモ #リファクタリング - Qiita
最終更新日 2018年08月15日 投稿日 2018年08月05日
EXCEL VBAでカラーのセットをセット #Excel - Qiita
最終更新日 2017年09月18日 投稿日 2017年09月17日
ExcelVBA開発時の不可解な挙動と対処法 #Excel - Qiita
最終更新日 2023年01月12日 投稿日 2020年06月11日
Excel VBA 第7回 文字色、もしくは、取り消し線で検索 #Excel - Qiita
最終更新日 2020年07月12日 投稿日 2020年05月26日
など

事例の前に

【ExcelVBA】条件付き書式はワークシート関数では使えない - 野口香社会保険労務士事務所
[記事公開]2023.03.01

条件付き書式の設定されたセルで、表示された色が何色かを把握するにはDisplayFormatを使いますが、DisplayFormatはユーザ定義関数では使えません。

ワークシート上で=CountRed(範囲)で呼び出そうとしたらエラーとなった
なんで?

原因
理由は、DisplayFormatがユーザ定義関数では機能しないからです。そういう仕様です。

Range.DisplayFormat プロパティ (Excel)

https://learn.microsoft.com/ja-jp/office/vba/api/excel.range.displayformat

↑これを読むと、「 ただし、DisplayFormat は、Visual Basic for Applications (VBA) から呼び出される関数では機能します。 」とありました。確かに、イミディエイトペインでは機能します。

以上。

ではさみしいので。以下事例。

事例

セルA1
=CountColoredCells1(A1:A366,6)

A列は「セルの書式設定」で黄色に。
20250422_231601.jpg

セルB1
=CountColoredCells2(B2:B366, 6)

B列は「条件付き書式」で黄色に。
20250422_231702.jpg
20250422_231800.jpg
セルC1は、マクロCountColoredCellsを実行した結果。(関数名を使いまわしたので)

Function CountColoredCells1(rng As Range, colorIndex As Integer) As Integer
'-------------------------------------------------------------------
' 関数: CountColoredCells1
' 説明: 指定した範囲内 (rng) のセルのうち、通常のセル色 (Interior.ColorIndex)
'       が指定した colorIndex と一致するセルの数をカウントして返します。
' 引数:
'   rng        - 対象となるセル範囲 (Range オブジェクト)
'   colorIndex - カウント対象のセル色 (ColorIndex の値、Integer)
' 戻り値:
'   一致するセルの個数 (Integer)
'
' 使用例: 黄色のセルを数える
'   Cell A1に以下を記載
'   =CountColoredCells1(A1:A366,6)
'-------------------------------------------------------------------
    Dim cell As Range
    Dim count As Integer
    count = 0
    Application.Volatile
    For Each cell In rng
        '通常のセル色をチェック
        If cell.Interior.colorIndex = colorIndex Then
            count = count + 1
        End If
    Next cell
    CountColoredCells1 = count
End Function

Function CountColoredCells2(rng As Range, colorIndex As Integer) As Integer
'-------------------------------------------------------------------
' 関数: CountColoredCells2
' 説明: 指定した範囲内 (rng) のセルのうち、条件付き書式の表示上のセル色
'       (DisplayFormat.Interior.ColorIndex) が指定した colorIndex と一致する
'       セルの数をカウントして返します。
' 引数:
'   rng        - 対象となるセル範囲 (Range オブジェクト)
'   colorIndex - カウント対象のセル色 (ColorIndex の値、Integer)
' 戻り値:
'   一致するセルの個数 (Integer)
'
' 使用例: 黄色のセルを数える
'   Dim count As Integer
'   count = CountColoredCells2(Range("B2:B366"), 6)
'-------------------------------------------------------------------
    Dim cell As Range
    Dim count As Integer
    count = 0
    Application.Volatile
    On Error Resume Next
    For Each cell In rng
        '条件付き書式の表示上の色をチェック
        If cell.DisplayFormat.Interior.colorIndex = colorIndex Then
            count = count + 1
        End If
    Next cell
    On Error GoTo 0
    CountColoredCells2 = count
End Function

Sub CountColoredCells()
    Cells(1, 3) = CountColoredCells2(Range("B2:B366"), 6)
End Sub

ちなみに、CountColoredCells2の「On Error Resume Next」と「On Error GoTo 0」がないとこんな感じになる。
20250422_231900.jpg

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?