Excel VBA 10行で書けるセル塗りつぶしお絵かきツール
あらかじめセルを好みの色で塗りつぶしたカラーパレットを用意し、そこから色を選んで、セルを塗りつぶすツールです。
セルをクリックして塗りつぶし、ダブルクリックで塗りつぶしを解除して無色にします。
ワークシートのB2~G2を好みの色で塗りつぶし、下記のコードをそのシートモジュールに貼り付けて使います。
ソースコード
'Copy the below code and paste it to a worksheet module
'Currently selected color
Private currentColor As Long
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'if the cell belongs to the color pallet
If Target.Row = 2 And Target.Column >= 2 And Target.Column <= 7 Then
'Set the selected color as currentlColor
currentColor = Target.Interior.ColorIndex
Else
'Fill the selected cell with currentColor
Target.Interior.ColorIndex = currentColor
End If
End Sub
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
'No fill color
Target.Interior.ColorIndex = -4142
End Sub