Since there has been no reply for a long time, let me reply for you.
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
For ease of explanation, Break it down into the following styles
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
Dim rng As Range
Set rng = Range("A1:D4")
If Not Intersect(Target, rng) Is Nothing Then
Dim s$, colorTgt$, arr, colorTmp$, colorRst&
'Values of three colors,Duplicate values are meant to facilitate the acquisition of next values
s = "16777215 65280 65535 16777215"
'Converting color values to strings, example: green = "65280"
colorTgt = CStr(Target.Interior.color)
'Converting strings into one-dimensional arrays using colorTgt as a delimiter
'example: colorTgt = "65280" ,then arr = array("16777215 "," 65535 16777215")
arr = Split(s, colorTgt)
'example: arr(1) = " 65535 16777215",The subscript of a one-dimensional array starts at 0
colorTmp = arr(1)
'example: colorTmp = "65535 16777215", Trim () Remove the header and tail spaces
colorTmp = Trim(colorTmp)
'Split(colorTmp) = array("65535","16777215"), Split(colorTmp)(0) = "65535"
'example: colorRst = 65535 (yellow), Split () is delimited by space by default when delimiters are omitted
colorRst = Split(colorTmp)(0)
'Colour cell areas use colorRst
rng.Interior.color = colorRst
'Do not pop up the right-click menu
Cancel = True
End If
End Sub