Macro to trigger upon Worksheet Deactivate, instead of Worksheet Change

scott_od

New Member
Joined
Jan 25, 2016
Messages
27
The code below uses the value in cells G2:G6 (worksheet name "Data"), whenever they are updated, to populate the data validation input message in worksheet "Overview" cells F4:F8
And the value in cells I2:I6 (worksheet name "Data"), to populate the data validation input message in worksheet "Overview" cells G4:G8

However, I would like to change the code so that it's triggered only when the user leaves the “Data” worksheet, by converting the code to a Worksheet Deactivate event.
Would appreciate anybody's help. Thank you.

"Data" Worksheet
1634636435524.png


"Overview" Worksheet
1634636507900.png


VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   
    If Target.CountLarge = 1 And Not Intersect(Target, Range("G2:G6")) Is Nothing Then
        With Worksheets("Overview").Range("F" & Target.Row + 2).Validation
            .Delete
            .Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator:=xlBetween
            .InputTitle = "Comment"
            .InputMessage = Target.Value
        End With
   
    ElseIf Target.CountLarge = 1 And Not Intersect(Target, Range("I2:I6")) Is Nothing Then
        With Worksheets("Overview").Range("G" & Target.Row + 2).Validation
            .Delete
            .Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator:=xlBetween
            .InputTitle = "Original Target Date"
            .InputMessage = Target.Value
        End With
    End If
   
End Sub
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
try this:
VBA Code:
Private Sub Worksheet_Deactivate()
    For Targetrow = 2 To 6
      TargetValue = Worksheets("Data").Range("G" & Targetrow)
        With Worksheets("Overview").Range("F" & Targetrow + 2).Validation
            .Delete
            .Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator:=xlBetween
            .InputTitle = "Comment"
            .InputMessage = TargetValue
        End With
   
      TargetValue = Worksheets("Data").Range("I" & Targetrow)
        With Worksheets("Overview").Range("G" & Targetrow + 2).Validation
            .Delete
            .Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator:=xlBetween
            .InputTitle = "Original Target Date"
            .InputMessage = TargetValue
        End With
    Next Targetrow

End Sub
 
Upvote 0
Solution
try this:
VBA Code:
Private Sub Worksheet_Deactivate()
    For Targetrow = 2 To 6
      TargetValue = Worksheets("Data").Range("G" & Targetrow)
        With Worksheets("Overview").Range("F" & Targetrow + 2).Validation
            .Delete
            .Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator:=xlBetween
            .InputTitle = "Comment"
            .InputMessage = TargetValue
        End With
  
      TargetValue = Worksheets("Data").Range("I" & Targetrow)
        With Worksheets("Overview").Range("G" & Targetrow + 2).Validation
            .Delete
            .Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator:=xlBetween
            .InputTitle = "Original Target Date"
            .InputMessage = TargetValue
        End With
    Next Targetrow

End Sub

One small bug that I have come across:

The "project overview" header is a shape, and if the shape is selected previously (before switching from the "Data" worksheet to the "Overview" worksheet), then I get runtime error 1004 'Application-defined or object defined error'

1634713759246.png

1634713891835.png
 
Upvote 0
I have no idea why that is happening, since this is unrelated to your original problem I suggest you start a new thread, one of the other Excel experts can hopefully help you
 
Upvote 0

Forum statistics

Threads
1,214,561
Messages
6,120,231
Members
448,951
Latest member
jennlynn

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