Cell selection macro

robertsmyth100

Board Regular
Joined
Sep 25, 2006
Messages
94
Hi,

I am trying to write a code which changes the colour of a cell (A1) when a cell in a range (E14:E15)is selected:

Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
If Target.Address = "$E$14:$E$15" Then
Range("A1").Select
With Selection.Interior
.ColorIndex = 0
.Pattern = xlSolid
End With
End If
End Sub

Could anyone help?
 

Some videos you may like

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Try

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
If Intersect(Target, Range("E14:E15")) Is Nothing Then Exit Sub
With Range("A1").Interior
    .ColorIndex = xlNone
    .Pattern = xlSolid
End With
End Sub
 

robertsmyth100

Board Regular
Joined
Sep 25, 2006
Messages
94
thankyou. How would I change the macro so that if another range, say D14:D15 is clicked then a different cell, A2, changes colour?
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650

ADVERTISEMENT

Try

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
If Not Intersect(Target, Range("E14:E15")) Is Nothing Then
    With Range("A1").Interior
        .ColorIndex = xlNone
        .Pattern = xlSolid
    End With
ElseIf Not Intersect(Target, Range("D14:D15")) Is Nothing Then
    With Range("A2").Interior
        .ColorIndex = xlNone
        .Pattern = xlSolid
    End With
End If
End Sub
 

robertsmyth100

Board Regular
Joined
Sep 25, 2006
Messages
94
thankyou.

One query - this code works in a new spreadsheet I created to test it but does not work on the spreadsheet I want to use it in. The spreadsheet I am trying to use it in already has macros that are working fine. Any idea why this might be?
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
You can only have one Worksheet_SelectionChange procedure per sheet. If you post your existing code we can probably combine them.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,481
Messages
5,596,399
Members
414,063
Latest member
N_Bates

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
Top