Toggle between colors on right click

jokr1

New Member
Joined
Jan 10, 2016
Messages
46
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
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
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]
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,928
Members
452,366
Latest member
TePunaBloke

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