Double click to change cell to multiple color or back to original

one4youman

New Member
Joined
Oct 5, 2018
Messages
13
Below is a piece of code from another thread where double clicking on a cell changes the cell to red. If you double click on the cell again it changes back to its original color format.

I am looking to see if there is a way to add additional colors to the choices. For Example, the first double click produces Green, second Red, third yellow, fourth gold, fifth white, and lastly the sixth would change it back to its original color.
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal target As Range, Cancel As Boolean)
'https://www.mrexcel.com/forum/excel-questions/634133-double-click-change-cell-colour.html
Static Fst      As Boolean
Static rng      As Range
Static ray
Dim c           As Long
Dim Dn          As Range
Dim Rw          As Long
    If Fst = False Then
        Set rng = ActiveSheet.UsedRange
        ReDim ray(1 To rng.Count, 1 To 2)
        For Each Dn In rng
            c = c + 1
            ray(c, 1) = Dn.Address: ray(c, 2) = Dn.Interior.ColorIndex
        Next Dn
        Fst = True
    End If
For Rw = 1 To UBound(ray)
    If ray(Rw, 1) = target.Address Then
        target.Interior.ColorIndex = IIf(target.Interior.ColorIndex = 3, ray(Rw, 2), 3)
    End If
Next Rw
End Sub
 
Last edited:

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal target As Range, Cancel As Boolean)
'https://www.mrexcel.com/forum/excel-questions/634133-double-click-change-cell-colour.html
Static Fst      As Boolean
Static rng      As Range
Static ray
Dim c           As Long
Dim Dn          As Range
Dim Rw          As Long
    If Fst = False Then
        Set rng = ActiveSheet.UsedRange
        ReDim ray(1 To rng.Count, 1 To 2)
        For Each Dn In rng
            c = c + 1
            ray(c, 1) = Dn.Address: ray(c, 2) = Dn.Interior.ColorIndex
        Next Dn
        Fst = True
    End If
For Rw = 1 To UBound(ray)
    If ray(Rw, 1) = target.Address Then
        target.Interior.ColorIndex = IIf(target.Interior.ColorIndex = 3, ray(Rw, 2), 3)
    End If
Next Rw
End Sub
 
Upvote 0
What other options for original color are there besides no fill? Could it be one of the other colors mentioned or something else entirely?
 
Upvote 0
Something like this. You can change or add colors or the sequence, but do allow for the Case Else so that odd colors will get the ball rolling.
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    With Target
        If Not Application.Intersect(.Cells, Me.Range("A1:W15")) Is Nothing Then
            With .Interior
                Select Case .Color
                    Case vbRed
                    .Color = vbBlue
                    Case vbBlue
                    .Color = vbGreen
                    Case vbGreen
                    .Color = vbYellow
                    Case Else
                    .Color = vbRed
                End Select
            End With
            Cancel = True
        End If
    End With
End Sub
 
Last edited:
Upvote 0
The cell might have a custom color shade already which is the reason to allow it loop back to the original color of the cell.
 
Upvote 0
The cell might have a custom color shade already which is the reason to allow it loop back to the original color of the cell.

That could get tricky if you interrupt the sequence on one cell and start clicking on another before going back to the first cell to finish clicking. Though, you could track the "default" colors on a hidden sheet and refer to that sheet to know what the original color was.
 
Upvote 0

Forum statistics

Threads
1,223,895
Messages
6,175,257
Members
452,625
Latest member
saadat28

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top