T Tim Smith Sep 27, 2003 #1 How can I get a macro to run automatically only when a specific cell e.g. A1 has its value changed? Many thanks.
How can I get a macro to run automatically only when a specific cell e.g. A1 has its value changed? Many thanks.
P philip Sep 27, 2003 #2 In VBA module in "ThisWorkbook" capture the SelectionChange event. If the address of the target is the correct cell address, then execute code. Example : Sub Worksheet_SelectionChange (ByVal Target as Range) If Target.Address = "$A$1" then ' put your code here end if End Sub
In VBA module in "ThisWorkbook" capture the SelectionChange event. If the address of the target is the correct cell address, then execute code. Example : Sub Worksheet_SelectionChange (ByVal Target as Range) If Target.Address = "$A$1" then ' put your code here end if End Sub
B Bob Phillips Sep 27, 2003 #3 Tim, Worksheet event code Private Sub Worksheet_Change(ByVal Target As Range) Application.EnableEvents = False On errot GoTo ws_exit If Not Intersect(Target, Range("A1")) Is Nothing Then 'your code here End If ws_exit: Application.EnableEvents = True End Sub Right-click on the sheet tab name, select the View Code option, and put the code in there. -- HTH Bob Phillips ... looking out across Poole Harbour to the Purbecks
Tim, Worksheet event code Private Sub Worksheet_Change(ByVal Target As Range) Application.EnableEvents = False On errot GoTo ws_exit If Not Intersect(Target, Range("A1")) Is Nothing Then 'your code here End If ws_exit: Application.EnableEvents = True End Sub Right-click on the sheet tab name, select the View Code option, and put the code in there. -- HTH Bob Phillips ... looking out across Poole Harbour to the Purbecks
B Bob Phillips Sep 27, 2003 #4 Philip, If you use Worksheet_SelectionChange event then it goes in the appropriate worksheet code module not ThisWorkbook. For ThisWorkbook, you would use Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range) and test the sheet as well. -- HTH Bob Phillips ... looking out across Poole Harbour to the Purbecks (remove nothere from the email address if mailing direct)
Philip, If you use Worksheet_SelectionChange event then it goes in the appropriate worksheet code module not ThisWorkbook. For ThisWorkbook, you would use Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range) and test the sheet as well. -- HTH Bob Phillips ... looking out across Poole Harbour to the Purbecks (remove nothere from the email address if mailing direct)