I made some correction to the code as shown below, but I am getting an error on this…
- User
I made some correction to the code as shown below, but I am getting an error on this line: Range("J1:O1").Find(What:=duplicateCell.Value, LookIn:=xlValues, LookAt:=xlWhole).Interior.Color = RGB(255, 0, 0)
Private Sub Worksheet_Change(ByVal Target As Range) Dim lastRow As Long Dim checkRange As Range Dim duplicateCell As Range
' Unlock the worksheet ActiveSheet.Unprotect Password:="edit" ' Check if Target is within the specified range If Target.CountLarge > 1 Then Exit Sub If Not Intersect(Target, Range("D4")) Is Nothing Then ' Call FindAndCopySupplierData Application.EnableEvents = False Call FindAndCopySupplierData Application.EnableEvents = True End If ' Check if Target is within the specified range If Target.CountLarge > 1 Then Exit Sub If Not Intersect(Target, Range("A11")) Is Nothing Then ' Update E12 with current timestamp Application.EnableEvents = False If Target.Value <> "" Then Range("E11").Value = Now End If Application.EnableEvents = True End If ' Calculate J1 to O1 based on B3 to B8 entries If Not Intersect(Target, Range("B3:B8")) Is Nothing Then Range("J1:O1").Value = WorksheetFunction.Transpose(Range("B3:B8").Value) End If ' Check for duplicate entries in J1 to O1 from row 1 to 700 lastRow = 700 Set checkRange = Range("J4:O" & lastRow) For Each duplicateCell In checkRange If WorksheetFunction.CountIf(checkRange, duplicateCell.Value) > 1 Then ' Mark duplicates in Row 1 If Application.CountIf(Rows(1), duplicateCell.Value) > 1 Then duplicateFound = True Range("J1:O1").Find(What:=duplicateCell.Value, LookIn:=xlValues, LookAt:=xlWhole).Interior.Color = RGB(255, 0, 0) Else ' Reset color if no duplicates are found in Row 1 Range("J1:O1").Interior.ColorIndex = xlNone End If End If Next duplicateCell ' Check if all entries are unique If WorksheetFunction.CountA(checkRange) = lastRow Then ' Make Button2 visible 'Sheet1.Buttons("Button2").Visible = True ' Ask user if they want to add the details to the Suppliers List Dim response As VbMsgBoxResult response = MsgBox("Do you want to add the details to the Suppliers List?", vbQuestion + vbYesNo) If response = vbYes Then ' Add details to Suppliers List Dim suppliersSheet As Worksheet Set suppliersSheet = ThisWorkbook.Sheets("Suppliers") With suppliersSheet lastRow = .Cells(.Rows.count, "A").End(xlUp).row + 1 ' Find next available row in Suppliers sheet .Cells(lastRow, "A").Value = Range("B3").Value .Cells(lastRow, "B").Value = Range("B4").Value .Cells(lastRow, "C").Value = Range("B5").Value .Cells(lastRow, "D").Value = Range("B6").Value .Cells(lastRow, "E").Value = Range("B7").Value .Cells(lastRow, "F").Value = Range("B8").Value MsgBox "New supplier's details added" & vbCrLf & "Remember to update List selection in" & vbCrLf & "Job Request B3" End With End If End If ' Lock the worksheet ActiveSheet.Protect Password:="edit" - Assistant
The error on the mentioned line is likely occurring because the
Findmethod did not find a match forduplicateCell.Valuein the range "J1:O1". This can happen if the value does not exist in that range.To avoid the error, you can add a check before changing the cell's interior color to make sure a match was found:
Dim foundCell As Range Set foundCell = Range("J1:O1").Find(What:=duplicateCell.Value, LookIn:=xlValues, LookAt:=xlWhole) If Not foundCell Is Nothing Then foundCell.Interior.Color = RGB(255, 0, 0) End IfThis way, the interior color will only be changed if a match is found.
- User
where will I add this in the code
- Assistant
You should add this code right after the line where you are getting the error. Here's the updated code with the correction:
For Each duplicateCell In checkRange If WorksheetFunction.CountIf(checkRange, duplicateCell.Value) > 1 Then ' Mark duplicates in Row 1 If Application.CountIf(Rows(1), duplicateCell.Value) > 1 Then duplicateFound = True Dim foundCell As Range Set foundCell = Range("J1:O1").Find(What:=duplicateCell.Value, LookIn:=xlValues, LookAt:=xlWhole) If Not foundCell Is Nothing Then foundCell.Interior.Color = RGB(255, 0, 0) End If Else ' Reset color if no duplicates are found in Row 1 Range("J1:O1").Interior.ColorIndex = xlNone End If End If Next duplicateCellBy adding this code, you are checking if the
Findmethod found a match forduplicateCell.Valuein the range "J1:O1", and only changing the interior color if a match is found.