Hi
Amending the code to change all cells to the left of the cell chosen is easy enough to achieve.
I am assuming that if you had 4 cells coloured Yellow in a row, and you then clicked on a cell in column C, you would want all cells turned black, not just column B and C, leaving D and E as Yellow
The included code, will now set everything in the row back to black if the selected cell is already Yellow, and will colour all cells up to and including the selected cell as Yellow, if the colour is already Black.
Whatever we do, is going to involve more than 1 click anyway.
There are various events that can be used to trigger off a reaction on a sheet.
Before Double Click - which I chose
Before Right Click - which would mean having to disable the normal right click menu you get when right clicking a cell, and I do not favour
Selection Change - which happens as soon as you select a different cell on the sheet.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim tc As Long, tr As Long
If Target.Count > 1 Then Exit Sub
If Not Intersect(Target, Range("B22:F50")) Is Nothing Then
tc = Target.Column
tr = Target.Row
Select Case Target.Row
Case 32, 33, 45, 46
Exit Sub
Case Else
Target = Chr(171)
If Target.Font.ColorIndex = 6 Then
Range(Cells(tr, 2), Cells(tr, 6)).Font.ColorIndex = 0
Cells(tr, 7) = 0
Else
Range(Cells(tr, 2), Cells(tr, tc)).Font.ColorIndex = 6
Cells(tr, 7) = tc - 1
End If
End Select
End If
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim tc As Long, tr As Long
If Target.Count > 1 Then Exit Sub
If Not Intersect(Target, Range("B22:F50")) Is Nothing Then
tc = Target.Column
tr = Target.Row
Select Case Target.Row
Case 32, 33, 45, 46
Exit Sub
Case Else
Target = Chr(171)
If Target.Font.ColorIndex = 6 Then
Range(Cells(tr, 2), Cells(tr, 6)).Font.ColorIndex = 0
Cells(tr, 7) = 0
Else
Range(Cells(tr, 2), Cells(tr, tc)).Font.ColorIndex = 6
Cells(tr, 7) = tc - 1
End If
End Select
End If
End Sub
If we use Selection change, then having moved to a cell, the above events would take place, but you would then need to move to another cell in order to get the change back again.
I personally still favour the double click method, but I have left both enabled in the attached file.
You can either Rem out the block of code you don't want, or delete it.
I hope that this helps
Bookmarks