Please can you end this VBA code by showing me in a pop up message what he row number is…
- User
Please can you end this VBA code by showing me in a pop up message what he row number is of the match in sheet 'Allocation'
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 Application.EnableEvents = False Cancel = True ' Prevent edit mode for I1 ' 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 End If
If Target.Column <> 8 Then Exit Sub ' Check if double clicked in column H and exit if not If Target.Column = 8 And Target.row > 3 Then ' Rest code for processing row in column H Application.EnableEvents = False Cancel = True ' Prevent edit mode
Dim returnSheet As Worksheet, allocSheet As Worksheet, auditSheet As Worksheet Set returnSheet = Sheets("Key Return") Set allocSheet = Sheets("Key Allocation") Set auditSheet = Sheets("Key Audit")
Dim rngCheck As Range Set rngCheck = Intersect(Target, auditSheet.Columns(8))
Dim auditRow As Range Set auditRow = auditSheet.Rows(Target.row)
' Confirmation message If MsgBox("Would you like to record details of a Lost or Returned Key?", vbYesNo) = vbNo Then Application.EnableEvents = True ThisWorkbook.Worksheets("Key Audit").Protect password:="edit" 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 Application.EnableEvents = True ThisWorkbook.Worksheets("Key Audit").Protect password:="edit" 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 Application.EnableEvents = True ThisWorkbook.Worksheets("Key Audit").Protect password:="edit" Exit Sub End If
- Assistant
MsgBox "The row number of the match in sheet 'Allocation' is: " & allocRow.Row
- User
When I first ran the code, the matching row numbers were different, but after a couple of runs, it kept defaulting to only finding the first row with the first insatnce of a matching value in column A. Is there something in memory that needs to be cleared each time the code runs
- Assistant
To prevent the code from finding the same matching row multiple times and defaulting to the first instance, you can clear the Find settings before initiating a new search. Here's how you can modify the code to clear the Find settings each time before searching for a matching row:
' Clear Find settings Set allocRow = Nothing ' Find matching row in "Key Allocation" sheet Set allocRow = allocSheet.Rows.Find(What:=auditRow.Range("A1").Value, LookIn:=xlValues, LookAt:=xlWhole) ' Check if matching row found If allocRow Is Nothing Then MsgBox "Matching row not found in 'Key Allocation' sheet.", vbExclamation Application.EnableEvents = True ThisWorkbook.Worksheets("Key Audit").Protect password:="edit" Exit Sub End If MsgBox "The row number of the match in sheet 'Allocation' is: " & allocRow.RowIn this modified code,
Set allocRow = Nothingclears the Find settings before searching for a matching row in the "Key Allocation" sheet. This should prevent the code from defaulting to the first instance of a matching value and always find the correct matching row. - User
the issue idoes not appear to be a memory problem. What I want the code to do is this. When I double click on a row in sheet 'Key audit' find the matching row in sheet 'Allocation' where the values from column A to H are exactly the same.
- Assistant
To find the matching row in the "Key Allocation" sheet where the values from column A to H are exactly the same as the selected row in the "Key Audit" sheet, you can modify the Find method as follows:
' Find matching row in "Key Allocation" sheet Set allocRow = Nothing Dim foundRow As Range For Each rw In allocSheet.UsedRange.Rows If Not Intersect(rw, auditRow) Is Nothing Then Set foundRow = Nothing On Error Resume Next Set foundRow = rw.SpecialCells(xlCellTypeConstants, xlTextValues) On Error GoTo 0 If Not foundRow Is Nothing And foundRow.Count = 8 Then ' 8 columns A to H If Application.WorksheetFunction.CountIf(rw, auditRow.Cells(1)) = 8 Then Set allocRow = rw Exit For End If End If End If Next rw If allocRow Is Nothing Then MsgBox "Matching row not found in 'Key Allocation' sheet.", vbExclamation Application.EnableEvents = True ThisWorkbook.Worksheets("Key Audit").Protect Password:="edit" Exit Sub End If - User
variable 'rw' not defined
- Assistant
I apologize for the oversight. To fix the issue with the 'rw' variable not being defined, you can declare it as a Range variable before using it in the loop. Here is the corrected code snippet:
' Find matching row in "Key Allocation" sheet Set allocRow = Nothing Dim rw As Range Dim foundRow As Range For Each rw In allocSheet.UsedRange.Rows If Not Intersect(rw, auditRow) Is Nothing Then Set foundRow = Nothing On Error Resume Next Set foundRow = rw.SpecialCells(xlCellTypeConstants, xlTextValues) On Error GoTo 0 If Not foundRow Is Nothing And foundRow.Count = 8 Then ' 8 columns A to H If Application.WorksheetFunction.CountIf(rw, auditRow.Cells(1)) = 8 Then Set allocRow = rw Exit For End If End If End If Next rw If allocRow Is Nothing Then MsgBox "Matching row not found in 'Key Allocation' sheet.", vbExclamation Application.EnableEvents = True ThisWorkbook.Worksheets("Key Audit").Protect Password:="edit" Exit Sub End IfBy declaring 'rw' as a Range variable at the beginning of the code snippet, you should no longer receive the 'variable not defined' error, and the code should run without issues.
- User
method 'Intersect' of object'_Global' failed, on line: If Not Intersect(rw, auditRow) Is Nothing Then
- Assistant
It seems that the 'Intersect' method is being used incorrectly in the context of comparing rows. To compare the values of two rows for an exact match, you can loop through each cell in the rows and check for equality. Here's an updated version of the code snippet without using the 'Intersect' method:
' Find matching row in "Key Allocation" sheet Set allocRow = Nothing Dim foundMatch As Boolean For Each rw In allocSheet.UsedRange.Rows foundMatch = True For i = 1 To 8 ' Checking columns A to H If rw.Cells(i).Value <> auditRow.Cells(i).Value Then foundMatch = False Exit For End If Next i If foundMatch Then Set allocRow = rw Exit For End If Next rw If allocRow Is Nothing Then MsgBox "Matching row not found in 'Key Allocation' sheet.", vbExclamation Application.EnableEvents = True ThisWorkbook.Worksheets("Key Audit").Protect Password:="edit" Exit Sub End IfIn this updated code, we iterate through each row in the "Key Allocation" sheet and compare the values of columns A to H with the corresponding values in the selected row from the "Key Audit" sheet. If all values match exactly, the 'allocRow' is set as the matching row. If no matching row is found, a message is displayed.
- User
variable i not defined
- Assistant
To resolve the 'variable i not defined' issue, you can declare the 'i' variable as an integer before using it in the loop. Here's the corrected code snippet:
' Find matching row in "Key Allocation" sheet Set allocRow = Nothing Dim foundMatch As Boolean Dim i As Integer For Each rw In allocSheet.UsedRange.Rows foundMatch = True For i = 1 To 8 ' Checking columns A to H If rw.Cells(i).Value <> auditRow.Cells(i).Value Then foundMatch = False Exit For End If Next i If foundMatch Then Set allocRow = rw Exit For End If Next rw If allocRow Is Nothing Then MsgBox "Matching row not found in 'Key Allocation' sheet.", vbExclamation Application.EnableEvents = True ThisWorkbook.Worksheets("Key Audit").Protect Password:="edit" Exit Sub End IfBy declaring 'i' as an integer variable at the beginning of the code snippet, you will no longer encounter the 'variable not defined' error, and the code should run without issues.
- User
rw.EntireRow.Copy returnSheet.Rows(returnSheet.Rows.Count).End(xlUp).Offset(1).EntireRow. Can this code be altered to copy the row from A to H and paste the row into A to H
- Assistant
To alter the code to copy only columns A to H from the row and paste them into columns A to H in the destination sheet, you can update the copy and paste lines as shown below:
rw.Range("A1:H1").Copy returnSheet.Rows(returnSheet.Rows.Count).End(xlUp).Offset(1).Range("A1").PasteSpecial Paste:=xlPasteValues Application.CutCopyMode = False ' Clear the copy modeBy specifying the range
("A1:H1")when copying the columns, and specifying the destination rangeRange("A1")when pasting, you can ensure that only columns A to H are copied and pasted in the respective locations. ThePasteSpecialmethod withxlPasteValueswill paste the values only without formatting or formulas. Lastly,Application.CutCopyMode = Falseclears the copy mode after pasting. - User
When I run this code, I am geting an error 'object required' on the line: auditSheet.Cells(newRow, 12).Value = returnCell.Value.Offset(0, -1).Value ' Column F
Sub MatchAndDisplay() Application.EnableEvents = False Dim auditSheet As Worksheet Dim allocSheet As Worksheet Dim returnSheet As Worksheet Dim auditValue As String Dim allocCell As Range Dim returnCell As Range Dim auditRow As Long Dim newRow As Long
Set auditSheet = ThisWorkbook.Sheets("Key Audit") Set allocSheet = ThisWorkbook.Sheets("Key Allocation") Set returnSheet = ThisWorkbook.Sheets("Key Return")
- Assistant
The error "Object Required" occurs because the syntax
returnCell.Value.Offset(0, -1).Valueis not appropriate. When you are copying data from another cell, you should directly access the value of that cell without using theOffsetmethod.To resolve the error, you can update the line to directly access the value of the cell in the previous column. Here's the corrected line of code:
auditSheet.Cells(newRow, 12).Value = returnCell.Offset(0, -1).Value ' Column FBy removing the
.ValueafterreturnCell, you are directly accessing the value in the cell one column to the left ofreturnCell, which should prevent the "Object Required" error. - User
Thanks all good
- Assistant
You're welcome! I'm glad to hear that it's all good now. If you have any more questions or need further assistance in the future, feel free to ask. Happy coding!