Home > front end >  Excel VBA Query: Want to uncolour a range of cells on right click
Excel VBA Query: Want to uncolour a range of cells on right click

Time:12-14

I am trying to uncolour a range of cells in a 2 x 10 range upon right clicking on a cell. My code currently is not working. Any assistance would be appreciated. I am very new to VBA

Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
  If Not Intersect(Target, Range("I9:J359")) Is Nothing Then
    If Target.Cells.Count > 1 Then Exit Sub

    Cancel = True
    Target.Offset(10, 1).Interior.ColorIndex = xlNone
  End If
End Sub

Or any advice to improve this function would also be appreciated

CodePudding user response:

Based on your comments, does this do what you want?

Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
    If Not Intersect(Target, Range("I9:J359")) Is Nothing Then
        If Target.Cells.Count > 1 Then Exit Sub
        
        Cancel = True
        Target.Worksheet.Range(Target.Address & ":" & Target.Offset(9, 1).Address).Interior.ColorIndex = xlNone
    End If
End Sub
  • Related