I am getting an error on this event - Target not defined: Private Sub…
- User
I am getting an error on this event - Target not defined: Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Me.Range("C5:C305")) Is Nothing Then Set lastTarget = TargetMsgBox "Double Click to open Task Reminder" End If End Sub
- Assistant
The error you are getting is likely because the
lastTargetvariable has not been declared or defined. Here is the updated code with the variable declaration:Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim lastTarget As Range ' Add this line to declare the variable
If Not Intersect(Target, Me.Range("C5:C305")) Is Nothing Then Set lastTarget = Target MsgBox "Double Click to open Task Reminder" End IfEnd Sub
Make sure to declare the
lastTargetvariable at the beginning of your code module or in a global scope if you need to access it from other procedures. - User
How can I write this vba code so that the Message Box opens only if the cell contains a date: Dim lastTarget As Range If Not Intersect(Target, Me.Range("C5:C305")) Is Nothing Then Set lastTarget = Target MsgBox "Double Click to open Task Reminder" End If
- Assistant
To ensure that the message box opens only if the cell contains a date, you can check if the value of the cell is a date using the
IsDatefunction. Here's the updated code:Dim lastTarget As Range
Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Not Intersect(Target, Me.Range("C5:C305")) Is Nothing Then Set lastTarget = Target If IsDate(lastTarget.Value) Then MsgBox "Double Click to open Task Reminder" End If End If End Sub
By adding the
If IsDate(lastTarget.Value) Thencondition, the message box will only appear if the selected cell contains a valid date value.