The following VBA codes does everything I require except for one detail. When I double…
- User
The following VBA codes does everything I require except for one detail. When I double click on any cell in column H below row 4, the row it processes is always row 4. What the code must do, is process the exact row of the cell in column H that I double clicked on.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Address = "1" Then Cancel = True ' Prevent edit mode for I1
Me.Unprotect password:="edit" Application.EnableEvents = False ' Clear specific ranges Range("A2:H2").ClearContents Range("I3").ClearContents Range("I4").ClearContents Range("A4:H300").ClearContents MsgBox "Page has been reset" Application.EnableEvents = True Me.Protect password:="edit"End If
If Target.Column <> 8 Then Exit Sub ' Check if double clicked in column H and exit if not If Target.row < 4 Or Target.Value = "" Then Exit Sub
If Target.Column = 8 Then ' Rest code for processing row in column H 'If Target.row > 4 Or Target.Value <> "" Then ' Set auditRow based on the double-clicked cell in column H 'Set auditRow = Sheets("Key Audit").Rows(Target.row) Set auditRow = Sheets("Key Audit").Rows(Target.row).Offset(0) ' Offset by 0 rows 'If Target.Column = 8 Then 'If Target.row > 4 Or Target.Value <> "" Then 'Cancel = True ' Prevent edit mode
' Set worksheets 'Dim auditRow As Range, allocRow As Range Dim returnSheet As Worksheet, allocSheet As Worksheet
Set auditRow = Sheets("Key Audit").Rows(Target.row) Set returnSheet = Sheets("Key Return") Set allocSheet = Sheets("Key Allocation")
' Confirmation message If MsgBox("Would you like to record details of a Lost or Returned Key?", vbYesNo) = vbNo Then Exit Sub End If
' Password storage cell Dim passwordCell As Range Set passwordCell = Range("J1")
' Password Retrieval Dim storedPassword As String storedPassword = passwordCell.Value
' Password Prompt Dim enteredPassword As String enteredPassword = InputBox("Enter Password to access Key Return sheet:", "Password Required")
' Check entered password If enteredPassword <> storedPassword Then MsgBox "Incorrect Password. Access Denied.", vbExclamation Cancel = True ' Prevent further actions Exit Sub End If
' Find matching row in "Key Allocation" sheet Set allocRow = allocSheet.Rows.Find(auditRow.Range("A1:H1"), LookIn:=xlValues, LookAt:=xlWhole)
' Check if matching row found If allocRow Is Nothing Then MsgBox "Matching row not found in 'Key Allocation' sheet.", vbExclamation Exit Sub End If
- Assistant
To fix the issue of always processing row 4 when double clicking on a cell in column H, you need to update the code to properly handle the Target row. You can replace the line
Set auditRow = Sheets("Key Audit").Rows(Target.row)with the following code:Set auditRow = Sheets("Key Audit").Rows(Target.Row)This change ensures that the
auditRowis set based on the row of the double-clicked cell in column H.Additionally, you should also remove the unnecessary conditions
If Target.row < 4 Or Target.Value = ""before setting theauditRow. The conditionIf Target.Column <> 8 Then Exit Subshould be sufficient to ensure that the code only runs for double-clicks in column H.By making these adjustments, the code should properly process the row corresponding to the double-clicked cell in column H.
- User
The double click should only work on colum H and below H3
- Assistant
To ensure that the double-click functionality only works in column H and below row 3 (H3), you can modify the code as follows:
Replace the existing line:
If Target.Column <> 8 Then Exit Sub ' Check if double clicked in column H and exit if notWith the following code:
If Target.Column <> 8 Or Target.Row < 4 Then Exit Sub ' Check if double clicked in column H and below row 3This modification adds an additional check to exit the subroutine if the clicked cell is not in column H or if it is above row 3. This change ensures that the double-click functionality will only apply to cells in column H and starting from row 4.
- User
When I double click below row 4, it is not processing the exact row I double clicked on. I processes row 4.
- Assistant
To ensure that the code processes the exact row that is double-clicked below row 4 in column H, you need to adjust how the
auditRowis set. Instead of setting it to the entire row of the target, you can specify the specific range within the row that you want to work with.Modify the following line in your code:
Set auditRow = Sheets("Key Audit").Rows(Target.row)To this updated code:
Set auditRow = Sheets("Key Audit").Rows(Target.Row).Range("A" & Target.Row & ":H" & Target.Row)This change will set the
auditRowrange to columns A to H for the specific row that was double-clicked in column H. This way, the code will process the data in the row that corresponds to the cell in column H that was double-clicked below row 4. - User
with the ammendment, it processed row 1
- Assistant
I see the issue. In order to make sure the correct row is processed when double-clicking in column H below row 4, we need to adjust the way we handle the
auditRowrange.Please replace the line:
Set auditRow = Sheets("Key Audit").Rows(Target.Row).Range("A" & Target.Row & ":H" & Target.Row)With the following updated code:
Set auditRow = Sheets("Key Audit").Rows(Target.Row - 3).Range("A" & Target.Row & ":H" & Target.Row)This change will offset the row number by 3 to account for the fact that you want to start processing the data from row 4 onward. The
Target.Row - 3calculation ensures that the correct row is selected for processing when double-clicking in column H below row 4. - User
It is still doing the same thing. Always processing row 4 even if my double click is below row 4
- Assistant
Here is an approach to resolve the issue you are facing with the code always processing row 4 even when the double-click is below row 4 in column H:
Replace the following line in the code:
Set auditRow = Sheets("Key Audit").Rows(Target.Row - 3).Range("A" & Target.Row & ":H" & Target.Row)With the updated code snippet:
Set auditRow = Sheets("Key Audit").Rows(Target.Row).Range("A" & Target.Row & ":H" & Target.Row)By making this modification, the
auditRowwill be set based on the row of the cell that is double-clicked in column H, ensuring that the data processing occurs for the correct row, regardless of its position below row 4.