Application-defined or object-defined error, on the line 'allocRow.Copy…
- User
Application-defined or object-defined error, on the line 'allocRow.Copy returnSheet.Rows(returnSheet.Rows.Count).End(xlUp).Offset(1).Range("A:H")' int he code : Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
ThisWorkbook.Worksheets("Key Allocation").Unprotect password:="edit" ThisWorkbook.Worksheets("Key Return").Unprotect password:="edit" ThisWorkbook.Worksheets("Key Audit").Unprotect password:="edit"
If Target.Address = "1" Then 'ThisWorkbook.Worksheets("Key Audit").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" Cancel = True ' Prevent edit mode for I1 Application.EnableEvents = True 'ThisWorkbook.Worksheets("Key Audit").Protect password:="edit" End If
If Target.Column <> 8 Or Target.row < 4 Then Exit Sub ' Check if double clicked in column H and exit if not If Target.Column = 8 Then ' Rest code for processing row in column H Application.EnableEvents = False 'ThisWorkbook.Worksheets("Key Audit").Unprotect password:="edit" ' Set worksheets Dim returnSheet As Worksheet, allocSheet As Worksheet, auditSheet As Worksheet ' Set auditRow based on the double-clicked cell in column H ' Set auditRow = Sheets("Key Audit").Rows(Target.row) Set auditSheet = Sheets("Key Audit") 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 ThisWorkbook.Worksheets("Key Audit").Protect password:="edit" Cancel = True ' Prevent further actions Application.EnableEvents = True 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 ThisWorkbook.Worksheets("Key Audit").Protect password:="edit" Cancel = True ' Prevent further actions Application.EnableEvents = True Exit Sub End If
'Set auditRow = Sheets("Key Audit").Rows(Target.row).Range("A" & Target.row & ":H" & Target.row) Set auditRow = auditSheet.Rows(Target.row).Range("A" & Target.row & ":H" & Target.row) ' Find matching row in "Key Allocation" sheet Set allocRow = allocSheet.Rows.Find(auditRow.Range("A1:H1"), LookIn:=xlValues, LookAt:=xlWhole)
- Assistant
It looks like the error is occurring on the line 'allocRow.Copy returnSheet.Rows(returnSheet.Rows.Count).End(xlUp).Offset(1).Range("A:H")'.
The error is likely caused by the way you are trying to copy the range from allocRow to returnSheet.
One possible solution is to separate the copy and paste actions into two separate lines, like this:
allocRow.Range("A:H").Copy returnSheet.Rows(returnSheet.Rows.Count).End(xlUp).Offset(1).Range("A:H").PasteSpecial xlPasteAllThis separates the copy action from the paste action, which may help resolve the error.
Additionally, make sure that all the necessary worksheets are still unprotected when executing the copy and paste actions.
If the issue persists, try debugging the code and checking the values of allocRow and returnSheet to ensure they are valid and contain the correct data before attempting to copy and paste the range.
Let me know if you need further assistance.
- User
how can i past values only
- Assistant
To paste only the values without formatting, formulas, or other properties, you can use the
PasteSpecialmethod with thexlPasteValuesparameter. Here is how you can modify the code to paste only the values:allocRow.Range("A:H").Copy returnSheet.Rows(returnSheet.Rows.Count).End(xlUp).Offset(1).Range("A:H").PasteSpecial Paste:=xlPasteValues Application.CutCopyMode = False ' Clear the clipboardBy using
PasteSpecial Paste:=xlPasteValues, you are pasting only the values from the copied range.After the paste operation, it's good practice to clear the clipboard by setting
Application.CutCopyMode = False.Make sure to test the modified code to ensure it pastes only the values as intended.
Let me know if you have any more questions or if you need further assistance.
- User
same error on this line: returnSheet.Rows(returnSheet.Rows.Count).End(xlUp).Offset(1).Range("A:H").PasteSpecial Paste:=xlPasteValues
- Assistant
If you are encountering an error on the
PasteSpecialline with thexlPasteValuesparameter, you can directly paste the values without using theCopyandPasteSpecialmethods. Here's how you can modify the code to achieve this:Dim targetRange As Range Set targetRange = returnSheet.Cells(returnSheet.Rows.Count, 1).End(xlUp).Offset(1, 0).Resize(1, 8) ' Resize to match columns A:H ' Copy the values from allocRow to the targetRange targetRange.Value = allocRow.Range("A:H").ValueIn this updated code snippet, the values from
allocRoware directly assigned to the target range inreturnSheet. This method bypasses the need for copying and pasting.After setting the values, ensure to clear the clipboard by setting
Application.CutCopyMode = False.Please replace the previous
allocRow.CopyandPasteSpeciallines with the above code snippet and test it to see if it resolves the error.Let me know if you continue to experience issues or if you need further assistance.
- User
error targetRange variable not defined
- Assistant
I apologize for the oversight. Let's define the
targetRangevariable properly in the code. Here's the corrected code snippet:Dim targetRange As Range Set targetRange = returnSheet.Cells(returnSheet.Rows.Count, 1).End(xlUp).Offset(1, 0).Resize(1, 8) ' Resize to match columns A:H ' Copy the values from allocRow to the targetRange targetRange.Value = allocRow.Range("A:H").Value Application.CutCopyMode = False ' Clear the clipboardIn the corrected code, the
targetRangevariable is properly defined before setting it to match the destination range for pasting the values. The values fromallocRoware then directly assigned to thetargetRange.Please replace the previous code snippet with this corrected version and test it to see if the error is resolved.
Let me know if you have any more questions or need further assistance.