Run a macro on a cell in specified range when cell changes

Joker4321

Board Regular
Joined
Jan 5, 2007
Messages
60
Hi, I have the following piece of code and I want to run it when a cell in a specified ranges is changed.

Private Sub worksheet_change(ByVal target As Range)


target.Interior.ColorIndex = 8
On Error Resume Next
Dim curcomment As String
curcomment = ""
curcomment = target.Comment.Text
If curcomment <> "" Then curcomment = curcomment & Chr(10)
target.AddComment
target.Comment.Text Text:=curcomment & ActiveWorkbook.BuiltinDocumentProperties("Author") & _
Chr(10) & "Rev. " & Format(Date, "dd-mmm-yyyy") & Format(Time, "hh:mm")
End Sub


Thanks in advance.
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Try

Code:
Private Sub worksheet_change(ByVal target As Range)

If Intersect(target, Range("A1:D12")) Is Nothing Then Exit Sub

target.Interior.ColorIndex = 8
On Error Resume Next
Dim curcomment As String
curcomment = ""
curcomment = target.Comment.Text
If curcomment <> "" Then curcomment = curcomment & Chr(10)
target.AddComment
target.Comment.Text Text:=curcomment & ActiveWorkbook.BuiltinDocumentProperties("Author") & _
Chr(10) & "Rev. " & Format(Date, "dd-mmm-yyyy") & Format(Time, "hh:mm")
End Sub

Change the range to suit in this line

Code:
If Intersect(target, Range("A1:D12")) Is Nothing Then Exit Sub
 

Joker4321

Board Regular
Joined
Jan 5, 2007
Messages
60
thanks for your help. Where should the code be placed in order to pickup the changes in the cells? i.e Workbook, module, pr worksheet?
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Right click the sheet tab, select View Code. Then copy the code and paste it into the white window on the right of the Visual Basic Editor. Close the VBE and try it out.
 

Joker4321

Board Regular
Joined
Jan 5, 2007
Messages
60

ADVERTISEMENT

works like a charm. Thank you!
 

Joker4321

Board Regular
Joined
Jan 5, 2007
Messages
60
is there a way to specify more than one range? As in if target is within range1 or range 2? I notice that if I use
If Intersect(target, Range("A1:D12"), Range("A15:D18")) Is Nothing Then Exit Sub

then it looks for an intersection of all 3. I tried using OR but that didn't work either
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Try

Code:
If Intersect(target, Union(Range("A1:D12"), Range("A15:D18"))) Is Nothing Then Exit Sub
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,152,164
Messages
5,768,561
Members
425,481
Latest member
ihumanl

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