VBA Runtime Error 1004 “Application-defined or Object-defined error”
is returned when I run this code. I searched Google / Stack Overflow and they all tell me to specify the range but funny thing is that the error occurred AFTER I specified range. (before the modification, it did work but returned wrong result)
Worksheets(2).Range(Cells(finderA.Row, rng2.Column), Cells(finderA.Row, rng2.Columns.Count + rng2.Column - 1)).Copy finalWS.Cells(rowCounter, rng1.Columns.Count + 2)
--> This is the line returning error.
The whole code is a part of my long lines for checking omissions between two data, by comparing specific common value.
What I'm basically trying to do in this code is to loop one range through the other and copy-paste the matching rows in order (i.e, I'm making sheet3 out of sheet 1,2.)
Dim rowCounter As Integer
rowCounter = 1
Dim eachCellA As Range, eachCellB As Range
Dim eachCellAa As Range, eachCellBb As Range
Dim StrFirstAdd As String, StrAdd As String
Dim finderA As Range, finderB As Range
For Each eachCellA In addCellRngA
With addCellRngB
Set finderA = .Find(eachCellA.Value, LookIn:=xlValues, Lookat:=xlWhole)
End With
If Not finderA Is Nothing Then
eachCellA.EntireRow.Copy finalWS.Cells(rowCounter, 1)
StrFirstAdd = finderA.Address
StrAdd = finderA.Address
Do
Worksheets(2).Range(Cells(finderA.Row, rng2.Column), Cells(finderA.Row, rng2.Columns.Count + rng2.Column - 1)).Copy finalWS.Cells(rowCounter, rng1.Columns.Count + 2) **##--> Debugger stops here, returns error.**
Set finderA = addCellRngB.FindNext(finderA)
StrAdd = finderA.Address
rowCounter = rowCounter + 1
Loop While Not finderA Is Nothing And StrAdd <> StrFirstAdd
Else
eachCellA.EntireRow.Copy finalWS.Cells(rowCounter, 1)
rowCounter = rowCounter + 1
End If
Next
This is the code and all the values (finderA.Row , rng2.Column , rng2.Columns.count etc) are NOT empty and have right values.
I thought to paste the whole lines were unnecessary but if you feel like the reason is not within the code, let me know I'll be more than happy to provide the whole code.