Changing cell color in a range of cells

mmdmalta

New Member
Joined
Oct 9, 2006
Messages
40
Hello, I have figured out the VBA formula to change cell color when double click, right click etc... however, i only want it to this on a specific section of the worksheet and not the whole worksheet. Here is what i entered in the VBA

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True
Target.Interior.Color = vbYellow
End Sub
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True
Target.Interior.Color = Cancel = False
Target.Interior.Color = vbGreen
End Sub

What do i need to do to get it to only do this in the range of a43 to e44?

Thank you for your help.
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
mmdmalta,

You might consider the following...

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Range("A43:E44")) Is Nothing Then
    Cancel = True
    Target.Interior.Color = vbYellow
End If
End Sub
Code:
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Range("A43:E44")) Is Nothing Then
    Cancel = True
    Target.Interior.Color = Cancel = False
    Target.Interior.Color = vbGreen
End If
End Sub

Cheers,

tonyyy
 
Upvote 0
Try:

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
 If Intersect(Target, [A43:E44]) Is Nothing Then Exit Sub
 Cancel = True
 Target.Interior.Color = vbYellow
End Sub
Code:
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
 If Intersect(Target, [A43:E44]) Is Nothing Then Exit Sub
 Cancel = True
 Target.Interior.Color = vbGreen
End Sub
 
Upvote 0
Tonyyy,

That worked!!! I was just missing the second parenthesis in the Range. Good to know. Thank you so much!!!.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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