I need to run a macro when the value in cell $O$2 changes.
I have a workbook with 100+ sheets each tracking an individual work ticket. Each sheet is exactly the same (except for work notes). I want to run a macro named, "Insert_NEW" when I hit ENTER and the cell value of $O$2 changes versus manually clicking a button to run the macro 100+ times a day. I know I'm overlooking something simple, but I've spent almost 4 hours trying to add this feature when it can probably be resolved in 1-2 minutes. The goal is to embed the updated script between the EnableEvent update true/false. Thank you in advance for any assistance.
I have this existing script:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 15 Then
Application.EnableEvents = False
Cells(Target.Row, 27).Value = Date + Time
Application.EnableEvents = True
End If
End Sub
I'm trying to add the script below to run the existing functional macro named "Insert_NEW".
If Target.Address = "$O$2" Then
Call Insert_NEW
End If
I've also tried
If Not Intersect(target, Range("$O$2") Is Nothing Then
Call Insert_NEW
End If
@TimWilliams - This is what I added as a module after removing the private subs from the rest of the workbook:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 15 Then
Application.EnableEvents = False
Cells(Target.Row, 27).Value = Date + Time
Application.EnableEvents = True
End If
If Not Intersect(Target, Range("$O$2")) Is Nothing Then
Call Insert_NEW
End If
End Sub
This works for me, in the worksheet code module for a single sheet:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
Set c = Application.Intersect(Target, Me.Range("O2"))
If Not c Is Nothing Then 'was O2 updated?
MsgBox "O2 changed"
Insert_NEW Me 'pass worksheet to your sub
End If
End Sub
If you want to track changes to O2 on any sheet in the workbook, remove any existing Worksheet_Change
code from individual worksheet modules, and add this code in the ThisWorkbook
code module:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim c As Range
Set c = Application.Intersect(Target, Sh.Range("O2"))
If Not c Is Nothing Then 'was O2 updated?
MsgBox "O2 changed on sheet '" & Sh.Name & "'"
Insert_NEW Sh 'pass worksheet to your sub
End If
End Sub
Your called sub goes in a regular code module:
Sub Insert_NEW(ws as Worksheet)
With ws
'...do stuf with `ws`
End With
End Sub
Make sure events are turned on: in a regular module run this -
Sub EventsOn
Application.EnableEvents = True
End sub