0
1

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.

ExcelVBAでカラーダイアログを使用する

Last updated at Posted at 2022-10-16

自分用備忘録として。
ダイアログのOKボタンを押すと「true」が返されるため、色を選択した場合の処理はif文の中に記載するのがポイント。そうしないと、キャンセルボタンを押したときとRGB(0,0,0)を選択したときの戻り値がどちらも「0」になる。

Sub ColorSelect()

    Dim COLOR As Long '戻り値。RGB値が10進数で返される
    Dim R As Integer
    Dim G As Integer
    Dim B As Integer

    'Showの引数→(Index,Red,Green,Brue)
    If Application.Dialogs(xlDialogEditColor).Show(1, 255, 50, 100) = True Then
        COLOR = ActiveWorkbook.Colors(1)

        '10進数で返された値を16進数に変換→2文字ずつ抽出→16進数を10進数に再変換
        R = WorksheetFunction.Hex2Dec(Right(Hex(COLOR), 2))
        G = WorksheetFunction.Hex2Dec(Mid(Hex(COLOR), 3, 2))
        B = WorksheetFunction.Hex2Dec(Left(Hex(COLOR), 2))

        MsgBox R & "," & G & "," & B

    End If

End Sub

実行結果

実行すると引数で指定した色が選択された状態でカラーダイアログが表示される。

ColorDialog.png

OKボタンを押すと設定した値のRGB値が返される。
ColorDialog2.png

参考:
https://kosapi.com/post-900/
https://vba-corner.livejournal.com/1691.html

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?