change event for cells within a range

M

MJKelly

Hi,

I want to run a macro if a cell within a certain range is changed by a
user.

eg, if a cell is changed within the range A1:A10 then the text color
of that cell changes from black to red.

can you help?

The only code I have so far is:-



Private Sub Worksheet_Change(ByVal Target As Range)

Target = Range("A1:A10")

End Sub

kind regards,
Matt
 
M

Mike H

Hi,

Try this

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1:A10")) Is Nothing Then
Target.Font.ColorIndex = 3
End If
End Sub

Mike
 
M

MJKelly

Mike,

That worked a treat. One further query, I want to paste data to the
range during a weekly setup. And do not want the event to trigger
until this has been done. How can I not have the event triggered in
this instance? The idea being that the original data is in grey text
and not bold and the amendments to the data are Black/Bold.

code now reads:-

Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Target, Range("A1:A10", "C1:C10")) Is Nothing Then
With Target
.Font.ColorIndex = 1
.Font.Bold = True
End With
End If

End Sub

Thanks,
Matt
 
M

Mike H

Hi,

If your pasting data then the chances are your pasting multiple cells so
this as a first line would work

If Target.Cells.Count > 1 Then Exit Sub

On the other hand a 100% catch all would be to modify the code thus

If Not Intersect(Target, Range("A1:A10", "C1:C10")) Is Nothing Then
response = MsgBox("Process Bolding?", vbYesNo)
If response = vbNo Then Exit Sub
With Target
.Font.ColorIndex = 1
.Font.Bold = True
End With
End If

End Sub

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top