ExcelVBAでは、セルや文字の色をセットすることもできますが、色をコード上で指定する必要があります。
この記事では、ExcelVBAでカラーダイアログを表示し、ユーザーに選択してもらった色をセットする方法を紹介します。
0. この記事のまとめ
カラーダイアログを表示して、選択した色を取得するには、以下のコードを使用します。
Application.Dialogs(xlDialogEditColor).Show (1)
intresult = ActiveWorkbook.Colors(1)
以下のコードで、カラーダイアログから選択された色を、セルにセットします。
Application.Dialogs(xlDialogEditColor).Show (1)
intresult = ActiveWorkbook.Colors(1)
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = intresult
.TintAndShade = 0
.PatternTintAndShade = 0
End With
1. カラーダイアログを表示する方法
カラーダイアログとは、これのことです。
これを表示するには、以下のコードを使用します。
Application.Dialogs(xlDialogEditColor).Show (1)
2. カラーダイアログに入力した色を取得する方法
上記のコードで指定された色を取得するには、以下のコードを使います。
intresult = ActiveWorkbook.Colors(1)
変数 intresult は、int型です。
3. 選択したセルに色をセットする。
選択セルは、Selectionオブジェクトから取得できます。
Selection.Interior.Color に先ほど取得したintresult をセットすることで、セルの色を設定できます。
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = intresult
.TintAndShade = 0
.PatternTintAndShade = 0
End With
リンク
コメント