1
2

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 1 year has passed since last update.

【永久保存版】Excel VBA Color 変更 チートシート CRUD 「色付け・色読み取り・リセット」

Last updated at Posted at 2019-06-10

概要

ExcelのVBAで色塗りをするときに、色の消し方を忘れがちです。
そのため、自分的に欲しかったチートシートを作りました。

C : Create : 色の設定
R : Read : 色の読み取り
U : Update : 色の更新
D : Delete: 色の削除
という感じで、
CRUD風にして見やすくしてみました。

過去の自分よ!これでラクをしてくれ!

対象

Dim myRange As Range
Dim myShape As Shape

塗りつぶし色

Create・Update

細かく指定したい場合はRGB、色名があらかじめわかっている場合は列挙子を利用します。

myRange.Interior.Color = Excel.XlRgbColor.rgbYellowGreen '黄緑
myRange.Interior.Color = VBA.Information.RGB(255, 255, 255) '白

Read

セルの塗りつぶし色を読み取ります。

Debug.Print(myRange.Interior.Color)

条件付き書式などで色が付いた結果の表示されているセルの塗りつぶし色を読み取ります。

Debug.Print(myRange.DisplayFormat.Interior.Color)

Delete

myRange.Interior.ColorIndex = Excel.xlNone
myRange.Interior.ColorIndex = Excel.XlColorIndex.xlColorIndexNone

※たぶんxlColorIndexを使ったほうがスマート

フォントの色

Create・Update

細かく指定したい場合はRGB、色名があらかじめわかっている場合は列挙子を利用します。

myRange.Font.Color = Excel.XlRgbColor.rgbYellowGreen '黄緑
myRange.Font.Color = VBA.Information.RGB(255, 255, 255) '白

Read

セルの文字色を読み取ります。

Debug.Print(myRange.Font.Color)

条件付き書式などで色が付いた結果の表示されているセルの文字色を読み取ります。

Debug.Print(myRange.DisplayFormat.Font.Color)

Delete

myRange.Font.ColorIndex = Excel.xlAutomatic
myRange.Font.ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic

※たぶんxlColorIndexを使ったほうがスマート

図形の塗りつぶし色

Create・Update

myShape.Fill.ForeColor.RGB = Excel.XlRgbColor.rgbYellowGreen '黄緑
myShape.Fill.ForeColor.RGB = VBA.Information.RGB(255, 255, 255) '白

Read

Debug.Print(myShape.Fill.ForeColor.RGB)

Delete

図形の新規作成時の色に戻します。テーマの色になります。

myShape.ShapeRange.ShapeStyle = msoShapeStylePreset9

図形の枠線色

Create・Update

myShape.Line.ForeColor.RGB = Excel.XlRgbColor.rgbYellowGreen '黄緑
myShape.Line.ForeColor.RGB = VBA.Information.RGB(255, 255, 255) '白

Read

Debug.Print(myShape.Line.ForeColor.RGB)

Delete

図形の新規作成時の色に戻します。テーマの色になります。

myShape.ShapeRange.ShapeStyle = msoShapeStylePreset9

RBGとColorIndexなど

Excel上にてテーマの色として表現されている色は、VBAではColorIndexで設定することができます。
image.png
テーマの色ObjectThemeColor、標準の色はSchemeColorとしてVBA上で使用できます。
image.png
Excel上にて色の設定で表現される色は、RGB()もしくは、XlRgbColorで設定できます。
image.png

ポイント

セルの色について、C・R・U はColorプロパティですが、D はColorIndexプロパティを操作します。
本記事では単色の場合のみを示しています。網掛けやグラデーション、半透明などの設定はまた機会があれば書きます。

Excelの機能「書式のコピー」を使って、セルと図形の間で書式のコピーペーストはできません。
おそらく、セルの枠線が Top, Left, Bottom, Right とで書式を変えられることが最大の要因と思われます。
しかし、塗りつぶし色だけでもコピーペーストできたほうが便利な気がする今日この頃です。

Excelsior!

1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?