getting run time error any one solve this

Prasad K

Board Regular
Joined
Aug 4, 2021
Messages
189
Office Version
  1. 2016
  2. 2007
Platform
  1. Windows
applied a worksheet change event code to find duplicate value with mobile number and then remove duplicate data in entire row & it's working perfectly here is a problem with i have getting run time error after duplicate data has been deleted on entire row

below i have given 3 screen shots


This is the code i have applied

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim arrList As Variant, cell As Range
Dim rowLast As Long, searchRow As Long

For Each cell In Target
    If cell.Column = 4 And Trim(cell.Value2) <> vbNullString Then
        rowLast = cell.Parent.Cells(cell.Parent.Rows.Count, 4).End(xlUp).Row
        arrList = cell.Parent.Range("D1:D" & rowLast).Value2
            For searchRow = LBound(arrList) To UBound(arrList)
                If searchRow <> cell.Row Then
                    If arrList(UBound(arrList), 1) = arrList(searchRow, 1) Then
                        cell.Parent.Activate
                        Union(cell, cell.Parent.Range("A" & searchRow & ":D" & searchRow)).Select
                        MsgBox "This Name and Mobile Number Already Exist in Row No- " & searchRow & _
                            chr(10) & chr(10) & chr(10) & "This is Duplicate Entry You Have Done Will be Now Removed..."
                        Application.EnableEvents = False
                        cell.EntireRow.Delete
                        Application.EnableEvents = True
                    End If
                End If
            Next searchRow
    End If
Next cell

End Sub
 

Attachments

  • s1.PNG
    s1.PNG
    41.5 KB · Views: 11
  • s2.PNG
    s2.PNG
    42 KB · Views: 12
  • s3.PNG
    s3.PNG
    27.3 KB · Views: 11

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
after you have deteleted the cell entire row, the cell object no longer exists. So looking for cell.Row returns an error.
Once you have found a duplicate value and deleted the row you don't need to continue the loop and check more rows so exit the loop at this point.
So try the code like this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim arrList As Variant, cell As Range
Dim rowLast As Long, searchRow As Long
Application.EnableEvents = False
For Each cell In Target
    If cell.Column = 4 And Trim(cell.Value2) <> vbNullString Then
        rowLast = cell.Parent.Cells(cell.Parent.Rows.Count, 4).End(xlUp).Row
        arrList = cell.Parent.Range("D1:D" & rowLast).Value2
            For searchRow = LBound(arrList) To UBound(arrList)
                If searchRow <> cell.Row Then
                    If arrList(UBound(arrList), 1) = arrList(searchRow, 1) Then
                        cell.Parent.Activate
                        Union(cell, cell.Parent.Range("A" & searchRow & ":D" & searchRow)).Select
                        MsgBox "This Name and Mobile Number Already Exist in Row No- " & searchRow & _
                            chr(10) & chr(10) & chr(10) & "This is Duplicate Entry You Have Done Will be Now Removed..."
                        cell.EntireRow.Delete
                        Exit For
                    End If
                End If
            Next searchRow
    End If
Next cell
Application.EnableEvents = True
End Sub
 
Upvote 0
after you have deteleted the cell entire row, the cell object no longer exists. So looking for cell.Row returns an error.
Once you have found a duplicate value and deleted the row you don't need to continue the loop and check more rows so exit the loop at this point.
So try the code like this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim arrList As Variant, cell As Range
Dim rowLast As Long, searchRow As Long
Application.EnableEvents = False
For Each cell In Target
    If cell.Column = 4 And Trim(cell.Value2) <> vbNullString Then
        rowLast = cell.Parent.Cells(cell.Parent.Rows.Count, 4).End(xlUp).Row
        arrList = cell.Parent.Range("D1:D" & rowLast).Value2
            For searchRow = LBound(arrList) To UBound(arrList)
                If searchRow <> cell.Row Then
                    If arrList(UBound(arrList), 1) = arrList(searchRow, 1) Then
                        cell.Parent.Activate
                        Union(cell, cell.Parent.Range("A" & searchRow & ":D" & searchRow)).Select
                        MsgBox "This Name and Mobile Number Already Exist in Row No- " & searchRow & _
                            chr(10) & chr(10) & chr(10) & "This is Duplicate Entry You Have Done Will be Now Removed..."
                        cell.EntireRow.Delete
                        Exit For
                    End If
                End If
            Next searchRow
    End If
Next cell
Application.EnableEvents = True
End Sub
Thankyou
 
Upvote 0

Forum statistics

Threads
1,213,510
Messages
6,114,044
Members
448,543
Latest member
MartinLarkin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top