vba code to delete duplicate rows in range causing error 1004

vba317

Board Regular
Joined
Oct 7, 2015
Messages
58
I am trying without success to write a vba procedure to delete a duplicate range of cells. The object of the procedure is to define one range of cells and if the code finds a match it should delete the range that matches. This worksheet was imported data from a text file.
The error is happening on the If Sheets("RawImport").Range(rng1).Value = rngFind Then line. Currently the code says the rngFind is Nothing but I know that a match exists on lines 460 - 478 on the RawImport Sheet. Any help is appreciated.

Code:
Dim lngLastRow As Long
Dim lngLoop1 As Long
Dim lngLoop2 As Long
Dim wrkbk As Excel.Workbook
Dim wrkShSrc As Excel.Worksheet
Dim wrkShDest As Excel.Worksheet
Dim lngRangeStart() As Variant
Dim lngRangeEnd() As Variant
Dim rng1 As Range
Dim rng2 As Range
Dim rng() As Variant
Dim rngFind As Range
Dim strRange As String
Dim strEnd1 As String


'On Error Resume Next


Set wrkbk = ActiveWorkbook
Set wrkShSrc = wrkbk.Worksheets("RawImport")
Set wrkShDest = wrkbk.Worksheets("Results")

lngLastRow = Cells(Rows.Count, "A").End(xlUp).Row



 Set rng1 = Range("A190:A208")strRange = "A209:A" & lngLastRow


Set rngFind = wrkShSrc.Range("A209:A" & lngLastRow).Find(What:=strRange, LookIn:=xlValues, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False)
    
    
If Sheets("RawImport").Range(rng1).Value = rngFind Then
    Sheets("RawImport").Range(rngFind).RemoveDuplicates Columns:=Array(1, 1), Header:=xlNo
    'Deletes all rows after A209
    Sheets("RawImport").Range(rngFind).EntireRow.Delete
End If
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Are you running the code with the sheet "RawImport" active? If not then rng1 is not on that sheet. The correct syntax for the errant line is simply: If rng1.value = rngFind.Value Then

but if rngFind is Nothing then the search term wasn't found.
 
Upvote 0

Forum statistics

Threads
1,215,375
Messages
6,124,591
Members
449,174
Latest member
chandan4057

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