\ ポイント最大11倍! /

【Excel】棒グラフの色を変更するVBA(ユーザーフォームを使用)

ユーザーフォームを使って、棒グラフの系列色を1クリックで変えるVBAです。

動画が見れない方は画像でご覧ください。

スポンサーリンク
目次

使い方

棒グラフの系列を選択してユーザーフォームのボタンを押すと、

その色に変わります。
円柱状に見えるようなグラデーションにしています。さらに、

2回連続して押すと色が濃くなります。お好みで使えます。

他の色に変更するときは「白」を押してから

他の色を押してください。

VBA

ユーザーフォームを作成し、コマンドボタンを挿入します。

コマンドボタンは上からCommand1,2…6としています。
CommandButtun実行時のVBAは下の通りです。

CommandButton1_Click()

 If TypeName(Selection) = "Range" Then
      
        MsgBox "グラフを選択してから実行してください。", 16
        Exit Sub
 End If
 With Selection.Border
        .Color = RGB(79, 129, 189)
    End With

    With Selection.Format.Fill
        .Visible = msoTrue
        .BackColor.RGB = RGB(149, 179, 215)
        .ForeColor.RGB = RGB(79, 129, 189)
        .TwoColorGradient msoGradientVertical, 3
    End With
End Sub

Private Sub CommandButton2_Click()
 If TypeName(Selection) = "Range" Then
      
        MsgBox "グラフを選択してから実行してください。", 16
        Exit Sub
    End If
 With Selection.Border
        .Color = RGB(192, 80, 77)
    End With

    With Selection.Format.Fill
        .Visible = msoTrue
        .BackColor.RGB = RGB(218, 150, 148)
        .ForeColor.RGB = RGB(192, 80, 77)
        .TwoColorGradient msoGradientVertical, 3
    End With
End Sub

Private Sub CommandButton3_Click()
 If TypeName(Selection) = "Range" Then
      
        MsgBox "グラフを選択してから実行してください。", 16
        Exit Sub
    End If
 With Selection.Border
        .Color = RGB(155, 187, 89)
    End With

    With Selection.Format.Fill
        .Visible = msoTrue
        .BackColor.RGB = RGB(196, 215, 155)
        .ForeColor.RGB = RGB(155, 187, 89)
        .TwoColorGradient msoGradientVertical, 3
    End With
End Sub

Private Sub CommandButton4_Click()
 If TypeName(Selection) = "Range" Then
      
        MsgBox "グラフを選択してから実行してください。", 16
        Exit Sub
    End If
 With Selection.Border
        .Color = RGB(128, 100, 162)
    End With

    With Selection.Format.Fill
        .Visible = msoTrue
        .ForeColor.RGB = RGB(128, 100, 162)
        .BackColor.RGB = RGB(177, 160, 199)
         .BackColor.RGB = RGB(177, 160, 199)
        .TwoColorGradient msoGradientVertical, 3
    End With
End Sub

Private Sub CommandButton5_Click()
 If TypeName(Selection) = "Range" Then
      
        MsgBox "グラフを選択してから実行してください。", 16
        Exit Sub
    End If
With Selection.Border
        .Color = RGB(247, 150, 70)
    End With

    With Selection.Format.Fill
        .Visible = msoTrue
        .BackColor.RGB = RGB(250, 191, 143)
        .ForeColor.RGB = RGB(247, 150, 70)
        .TwoColorGradient msoGradientVertical, 3
    End With
End Sub

Private Sub CommandButton6_Click()
 If TypeName(Selection) = "Range" Then
      
        MsgBox "グラフを選択してから実行してください。", 16
        Exit Sub
    End If
    With Selection.Format.Line
        .ForeColor.ObjectThemeColor = msoThemeColorText1
    End With
    With Selection.Format.Fill
        .ForeColor.ObjectThemeColor = msoThemeColorAccent1
        .ForeColor.ObjectThemeColor = msoThemeColorBackground1
        .Solid

    End With
End Sub
あわせて読みたい
【Excel】VBAのユーザーフォームを作成、表示する 「ユーザーフォーム」とは マクロをボタンに割り当て、ボタンを押すと決められたマクロを実行するテキストやコードの入力補助 など、マクロを動作させるのに便利なツー...
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!
目次