プロが教えるわが家の防犯対策術!

B1のセルからY3のセルまでの表を作っています。

やりたいことは、表の左のA列
例えばA1をWクリックで、B1からY1までに色をつける、もう一度Wクリックで元に戻す。

というのと、

表の中のそれぞれのセル、
例えばB1をWクリックしたら、色をつける、もう一度Wクリックで色を元に戻す

という両方です。

片方ずつなら、なんとかできるようになったのですが、
上記の2つを同時に組み込む組み合わせ方がわかりません。

どなたかわかる方はいらっしゃいますか?
よろしくお願いします。

A 回答 (2件)

worksheetイベントなので、発動したいシートに記入をして下さい


「色をつける」を「塗りつぶしをする」と解釈したので、文字色はそのままです。
文字色のみ変更をしたいのであれば、適宜修正してください

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

If (Target.Column = 1) And (Target.Row <= 3) Then
If Cells(Target.Row, 2).Interior.ColorIndex = xlNone Then
Range(Cells(Target.Row, 2), Cells(Target.Row, 25)).Interior.Color = RGB(120, 60, 0)
ElseIf Cells(Target.Row, 2).Interior.ColorIndex <> xlNone Then
Range(Cells(Target.Row, 2), Cells(Target.Row, 25)).Interior.ColorIndex = xlNone
End If
ElseIf (1 < Target.Column) And (Target.Column <= 25) And (Target.Row <= 3) Then
If Cells(Target.Row, Target.Column).Interior.ColorIndex = xlNone Then
Cells(Target.Row, Target.Column).Interior.Color = RGB(0, 120, 60)
ElseIf Cells(Target.Row, Target.Column).Interior.ColorIndex <> xlNone Then
Cells(Target.Row, Target.Column).Interior.ColorIndex = xlNone
End If
End If


End Sub
    • good
    • 0
この回答へのお礼

ありがとう

思っていたものができました。
ありがとうございました。

お礼日時:2017/10/10 16:43

こんばんは!



ダブルクリックがA列の場合はB~Y列すべてに適応し、
B~Y列の場合は対象セルだけに適応すれば良いのですね?

A列をダブルクリックした場合、B~Y列に一つでも色付きセルがあった場合はすべてのセルの塗りつぶしをなくすようにしています。

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim j As Long, myFlg As Boolean
If Intersect(Target, Range("A:Y")) Is Nothing Then Exit Sub
Cancel = True
With Target
If .Column = 1 Then
For j = 2 To Range("Y1").Column
If Cells(.Row, j).Interior.ColorIndex <> xlNone Then
myFlg = True
Exit For
End If
Next j
With Range(Cells(.Row, "B"), Cells(.Row, "Y"))
If myFlg = True Then
.Interior.ColorIndex = xlNone
Else
.Interior.ColorIndex = 6 '//←黄色にしています//
End If
End With
Else
If .Interior.ColorIndex = xlNone Then
.Interior.ColorIndex = 6
Else
.Interior.ColorIndex = xlNone
End If
End If
End With
End Sub

※ 色は「黄色」に統一しています。
こんな感じではどうでしょうか?m(_ _)m
    • good
    • 0
この回答へのお礼

ありがとう

完成しました。
ありがとうございました。

お礼日時:2017/10/10 16:43

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!