If I doubleclick on a cell that is bot in column 5, the code below runs. How can I stop…
- User
If I doubleclick on a cell that is bot in column 5, the code below runs. How can I stop this. I only want the code to run based on the following parameters; 'If Target.Column = 5 And Target.row >= 2 And Target.Value <> "" Then'
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) If Target.Column = 5 And Target.row >= 2 And Target.Value <> "" Then If Not Target.Comment Is Nothing Then MsgBox "QUOTE already copied to PREQUEST", vbCritical, "QUOTE" Cancel = True Exit Sub Else Dim nextRow As Long nextRow = Sheets("PREQUEST").Cells(Rows.count, 1).End(xlUp).row + 1
Me.Unprotect Password:="edit" Dim currentRow As Long currentRow = Target.row Dim ws As Worksheet Set ws = ThisWorkbook.Sheets("PREQUEST") ' Copy each cell separately to PREQUEST Dim i As Integer For i = 1 To 7 ' Columns A to G Target.Worksheet.Cells(currentRow, i).Copy Destination:=ws.Cells(nextRow, i) Application.Wait (Now + TimeValue("0:00:01")) Next i MsgBox "QUOTE copied to PREQUEST" & vbCrLf & vbCrLf & "Enter SPECIAL NOTES - column H" & vbCrLf & "to complete and send to Finance", vbInformation, "QUOTE" Target.AddComment "Copied to PREQUEST - " & Date Me.Protect Password:="edit" Cancel = True End If End If Sheets("PREQUEST").ActivateEnd Sub
- Assistant
To ensure that the code only runs if the user double clicks on a cell in column 5 with a row greater than or equal to 2 and with a non-empty value, you can modify the
Worksheet_BeforeDoubleClickevent code as follows:Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) If Not Intersect(Target, Me.Columns(5)) Is Nothing And Target.row >= 2 And Target.Value <> "" Then If Not Target.Comment Is Nothing Then MsgBox "QUOTE already copied to PREQUEST", vbCritical, "QUOTE" Cancel = True Exit Sub Else Dim nextRow As Long nextRow = Sheets("PREQUEST").Cells(Rows.count, 1).End(xlUp).row + 1 Me.Unprotect Password:="edit" Dim currentRow As Long currentRow = Target.row Dim ws As Worksheet Set ws = ThisWorkbook.Sheets("PREQUEST") ' Copy each cell separately to PREQUEST Dim i As Integer For i = 1 To 7 ' Columns A to G Target.Worksheet.Cells(currentRow, i).Copy Destination:=ws.Cells(nextRow, i) Application.Wait (Now + TimeValue("0:00:01")) Next i MsgBox "QUOTE copied to PREQUEST" & vbCrLf & vbCrLf & "Enter SPECIAL NOTES - column H" & vbCrLf & "to complete and send to Finance", vbInformation, "QUOTE" Target.AddComment "Copied to PREQUEST - " & Date Me.Protect Password:="edit" Cancel = True End If End If Sheets("PREQUEST").Activate End Sub - User
When I double click outside of column 5 the code still runs
- Assistant
If you want the code to run only when double-clicking on cells in column 5, you can modify the condition in the
Worksheet_BeforeDoubleClickevent to check specifically for column 5. Here is the updated code:Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) If Target.Column = 5 And Target.row >= 2 And Target.Value <> "" Then If Not Target.Comment Is Nothing Then MsgBox "QUOTE already copied to PREQUEST", vbCritical, "QUOTE" Cancel = True Exit Sub Else Dim nextRow As Long nextRow = Sheets("PREQUEST").Cells(Rows.count, 1).End(xlUp).row + 1 Me.Unprotect Password:="edit" Dim currentRow As Long currentRow = Target.row Dim ws As Worksheet Set ws = ThisWorkbook.Sheets("PREQUEST") ' Copy each cell separately to PREQUEST Dim i As Integer For i = 1 To 7 ' Columns A to G Target.Worksheet.Cells(currentRow, i).Copy Destination:=ws.Cells(nextRow, i) Application.Wait (Now + TimeValue("0:00:01")) Next i MsgBox "QUOTE copied to PREQUEST" & vbCrLf & vbCrLf & "Enter SPECIAL NOTES - column H" & vbCrLf & "to complete and send to Finance", vbInformation, "QUOTE" Target.AddComment "Copied to PREQUEST - " & Date Me.Protect Password:="edit" Cancel = True End If End If Sheets("PREQUEST").Activate End Sub - User
I believe it is the 'If Not Target.Comment Is Nothing ' that is causing the code to also run when I click outde of column 5. The 'If Not Target.Comment Is Nothing ' mus also only relate to column 5
- Assistant
To ensure that the condition
If Not Target.Comment Is Nothingonly applies to cells in column 5, you can add an additional check within the main condition block. This check will verify if the cell is in column 5 before evaluating the comment.Here is the updated code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) If Target.Column = 5 And Target.row >= 2 And Target.Value <> "" Then If Target.Column = 5 And Not Target.Comment Is Nothing Then MsgBox "QUOTE already copied to PREQUEST", vbCritical, "QUOTE" Cancel = True Exit Sub Else Dim nextRow As Long nextRow = Sheets("PREQUEST").Cells(Rows.count, 1).End(xlUp).row + 1 Me.Unprotect Password:="edit" Dim currentRow As Long currentRow = Target.row Dim ws As Worksheet Set ws = ThisWorkbook.Sheets("PREQUEST") ' Copy each cell separately to PREQUEST Dim i As Integer For i = 1 To 7 ' Columns A to G Target.Worksheet.Cells(currentRow, i).Copy Destination:=ws.Cells(nextRow, i) Application.Wait (Now + TimeValue("0:00:01")) Next i MsgBox "QUOTE copied to PREQUEST" & vbCrLf & vbCrLf & "Enter SPECIAL NOTES - column H" & vbCrLf & "to complete and send to Finance", vbInformation, "QUOTE" Target.AddComment "Copied to PREQUEST - " & Date Me.Protect Password:="edit" Cancel = True End If End If Sheets("PREQUEST").Activate End Sub