
In Step 4, we simply close out the If statement. If there is an intersection, Step 3 fires the Save method of the active workbook, overwriting the previous version. Step 2 forces the macro to stop and exit the procedure if there is no intersection between the target cell and the specified range. A value of Nothing means the target cell falls outside the range specified. In Step 1, we are simply checking to see if the target cell (the cell that has changed) is in the range specified by the Intersect method. At that point, we can make the decision whether to save the workbook. So in essence, we need to throw the target cell against the Intersect method to check for a value of Nothing. The Intersect method returns one of two things: either a Range object that defines the intersec- tion between the two given ranges, or nothing. Because we don’t want to save the worksheet when any old cell changes, we use the Intersect method to determine if the target cell (the cell that changed) intersects with the range we have specified to be the trigger range (A5:A10 in this case). #Excel vba on cell change event code
The secret to this code is the Intersect method.
'Step 3: If there is an intersection, save the workbook 'Step 2: If there is no intersection, exit procedure If Intersect(Target, Range("A5:A10")) Is Nothing Then 'Step 1: Does the changed range intersect specified range?
Macro Code to Saving a Workbook When a Specific Cell Is Changed Private Sub Worksheet_Change(ByVal Target As Range) 1 Macro Code to Saving a Workbook When a Specific Cell Is Changed.