Toggle between colors on right click

jokr1

New Member
Joined
Jan 10, 2016
Messages
43
When right clicking within cell range A1 : D4, cell turns green.

I want to expand further on this and add that when right clicking on green cell, cell turns yellow. And when right clicking on yellow, cell turns white.

How would that look like?

And is there a cleaner way by not using Elseif (like switch?)

Code:
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
    Dim rInt As Range
    Dim rCell As Range
       Set rInt = Intersect(Target, Range("A1:D4"))
    If Not rInt Is Nothing And Target.Interior.ColorIndex = 0 Then
        For Each rCell In rInt
        Next
    End If
    Set rInt = Nothing
    Set rCell = Nothing
Cancel = True
Target.Interior.ColorIndex = 4
End Sub
 

Some videos you may like

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,334
Office Version
  1. 2010
Platform
  1. Windows
Try using this event code instead of the one you posted...
Code:
[table="width: 500"]
[tr]
	[td]Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
  If Not Intersect(Target, Range("A1:D4")) Is Nothing Then
    Target.Interior.Color = Split(Trim(Split("16777215 65280 65535 16777215", CStr(Target.Interior.Color))(1)))(0)
    Cancel = True
  End If
End Sub[/td]
[/tr]
[/table]
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
46,578
Office Version
  1. 365
Platform
  1. Windows
Cross posted http://www.vbaexpress.com/forum/showthread.php?64843-If-Elseif-to-toggle

While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules).
This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
 

Watch MrExcel Video

Forum statistics

Threads
1,109,444
Messages
5,528,798
Members
409,835
Latest member
Mafu1267

This Week's Hot Topics

  • Change military grades into rank
    Afternoon all Need help with formula that will change military rank (i.e. 1, 2, 3 into Amn, A1C, SrA). Running IF formula that does not work...
  • VBA COUNTIF SOLUTION
    Hi The following are the errors spread across the several columns from E to Q ie. 13 columns across several sheets with more than 500 rows per...
  • INSERT ROW WITH SPECIFIS TEXT IN A COLUMN
    Hi All! How can identify that that the row to be inserted has to be inserted before 1st row with specific text in column F. If I record the...
  • Auto-Create a monthly Sign in sheet for preschool students
    The image below is what each page looks like. Above is space for the "Child Name" "Month" "Class" School days are obviously Monday-Friday but...
  • VBA vlookup multiple results
    Hi folks, Hopefully someone out there can help. I have a list to vlookup which works (ish). the lookup only picks up the first instance of the...
  • Extract values for earliest/latest times
    I am trying to put together a formula to get the earliest start time, the latest end time from column A for each person in Column B-F without the...
Top