If Target.CountLarge > 1 Then Exit Sub If Target.Column = 7 Then 'Check if clicked cell…
- User
If Target.CountLarge > 1 Then Exit Sub If Target.Column = 7 Then 'Check if clicked cell is in column 7 - G If Not IsDate(Target.Value) Then MsgBox "Service Due Date needs to be present" . . . In this code, the Date related to the Target is offset to the right on the same row by 1 cell. How can I write this
- Assistant
You can use the Offset property to refer to the cell on the same row but 1 cell to the right of the clicked cell. Here's how you can modify the code to display the value of the cell on the right in the message box:
If Target.CountLarge > 1 Then Exit Sub If Target.Column = 7 Then 'Check if clicked cell is in column 7 - G If Not IsDate(Target.Value) Then MsgBox "Service Due Date needs to be present" & vbCrLf & "Date: " & Target.Offset(0, 1).Value End If End IfThe
Target.Offset(0, 1)refers to the cell on the same row, but 1 cell to the right of the clicked cell. You can then concatenate it with the message using&. The& vbCrLf &adds a line break between the original message and the value of the offset cell for better readability. - User
This is what I am trying to achieve, will this code work: If Target.CountLarge > 1 Then Exit Sub If Target.Column = 7 Then 'Check if clicked cell is in column 7 - G If Not IsDate(Target.Offset(0, 1).Value) Then MsgBox "Service Due Date needs to be present" End If End If
- Assistant
Yes, the code you provided will work for the desired outcome.
If the count of the selected cells is greater than 1, the code will exit the sub without executing the remaining code. Otherwise, it will check if the clicked cell is in column 7. If it is, it will then verify if the value in the cell on the same row, but 1 cell to the right, is not a date. If it is not a date, it will display the message "Service Due Date needs to be present" in a message box.
So, this code will correctly check if the value in the cell to the right of the clicked cell is not a date and display the message accordingly.
- User
Can you please help me correct this code: Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.CountLarge > 1 Then Exit Sub
If Target.Column = 3 Then 'Check if clicked cell is in column 3 - C GoTo TaskReminder
If Target.Column = 7 Then 'Check if clicked cell is in column 7 - G GoTo ServiceReminder
If Target.Column = 9 Then 'Check if clicked cell is in column 9 - I GoTo Thankyou
TaskReminder: If Not IsDate(Target.Offset(0, 6).Value) Then MsgBox "Task Date shows as Completed" Else Call OpenTaskReminder End If Exit Sub
ServiceReminder: If Not IsDate(Target.Offset(0, 1).Value) Then MsgBox "Service Due Date needs to be present" Else Call OpenServiceReminder End If Exit Sub
Thankyou: If Not IsDate(Target.Value) Then MsgBox "Task Completed Date needs to be present" Else Call OpenThankYou End If Exit Sub
End If End If End If
End Sub
- Assistant
Here's the corrected code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) If Target.CountLarge > 1 Then Exit Sub If Target.Column = 3 Then 'Check if clicked cell is in column 3 - C TaskReminder Target ElseIf Target.Column = 7 Then 'Check if clicked cell is in column 7 - G ServiceReminder Target ElseIf Target.Column = 9 Then 'Check if clicked cell is in column 9 - I Thankyou Target End If End Sub Private Sub TaskReminder(ByVal Target As Range) If Not IsDate(Target.Offset(0, 6).Value) Then MsgBox "Task Date shows as Completed" Else Call OpenTaskReminder End If End Sub Private Sub ServiceReminder(ByVal Target As Range) If Not IsDate(Target.Offset(0, 1).Value) Then MsgBox "Service Due Date needs to be present" Else Call OpenServiceReminder End If End Sub Private Sub Thankyou(ByVal Target As Range) If Not IsDate(Target.Value) Then MsgBox "Task Completed Date needs to be present" Else Call OpenThankYou End If End Sub - User
Private Sub TaskReminder(ByVal Target As Range) If Not IsDate(Target.Offset(0, 6).Value) Then MsgBox "Task Date shows as Completed" Else Call OpenTaskReminder End If End Sub . . . In this code I would like to change it so that it does not look for a date but that if the offset value is NOT blank then the message will be shown
- Assistant
To modify the code to check if the offset value is not blank instead of checking if it's a date, you can replace the
IsDatefunction with theIsEmptyfunction. Here's the updated code:Private Sub TaskReminder(ByVal Target As Range) If Not IsEmpty(Target.Offset(0, 6).Value) Then MsgBox "Task Date shows as Completed" Else Call OpenTaskReminder End If End SubWith this modification, if the value in the cell that is 6 columns to the right of the clicked cell is not blank, the message "Task Date shows as Completed" will be displayed. Otherwise, the
OpenTaskRemindersubroutine will be called.Remember to adjust the other parts of the code accordingly if needed.