VBA for changing cell's properties within given range

Hi, what I'm trying to achieve is changing cell's content, color etc. when selecting (clicking on) that cell. I need to do this in particular range.

 

Basically I need something like this, but for any cell within given range:

 

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Not Intersect(Range("A1"), Target) Is Nothing Then

     If Not (Application.Range("A1").Value = "X") Then

          Application.Range("A1").Value = "X"

          Range("A1").Interior.ColorIndex = 37

     Else

          Application.Range("A1").Value = ""

          Range("A1").Interior.ColorIndex = 0

     End If

End If

End Sub

 

I am new to VBA so I would really appreciate your help. Thanks a lot :-)

Nick's picture

easy one:Private Sub

easy one:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

' 'Target' is the cell you select
' you might want to rule out cases where you select multiple cells
'so: if Target.rows.count >0 then exit sub
'and: if Target.columns.count >0 then exit sub

If Not Intersect(Range("A1:B3"), Target) Is Nothing Then ' set the range to whatever you like e.g. 'A1:B3'

If Not (Target.Value = "X") Then

Target.Value = "X"

Target.Interior.ColorIndex = 37

Else

Target.Value = ""

Target.Interior.ColorIndex = 0

End If

End If

End Sub