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

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
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
 
Upvote 0
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?
 
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0
Try

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

Forum statistics

Threads
1,214,869
Messages
6,122,012
Members
449,060
Latest member
LinusJE

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