I have some code that works when isolated to a separate workbook, but when integrating it into my project, there is an issue with the loop. Sheet, column and range references are identical from testing to runtime. The objective is for data entered into a table being copied to another worksheet. During runtime, the loop starts with the first for, however, upon second iteration, I get an object variable not set error on
I'm at a complete loss since I don't know why it works for one situation, but not the other. Can anyone see something that I could be overlooking?
Code:
dws.Range("B" & dlr).value = cell.value
Code:
Public Sub PasteData()
Dim sws As Worksheet, dws As Worksheet
Dim tbl As ListObject
Dim CodeCell As Range, BottomCell As Range
Dim dlr As Long
Dim chkStr As String
Set wb = ThisWorkbook
Set sws = wb.Sheets(1)
Set dws = wb.Sheets(2)
Set tbl = sws.ListObjects("Table")
Set BottomCell = dws.Range("B:B").Find(what:="This is the bottom", lookat:=xlWhole)
' On Error GoTo Err
Application.ScreenUpdating = False
For Each cell In tbl.DataBodyRange.Columns(2).Cells
If cell <> "" Then
chkStr = cell.value & cell.Offset(0, 1).value & cell.Offset(0, 3).value & cell.Offset(0, 4).value & cell.Offset(0, 5).value & _
cell.Offset(0, 2).value & cell.Offset(0, 6).value
If Not InvoiceFound(chkStr) Then
If dws.Range("B17").value = "" Then
dlr = 17
Else
Set CodeCell = dws.Range("B:B").Find(what:="Baseline", after:=dws.Range("B16"), lookat:=xlWhole)
dlr = CodeCell.End(xlUp).Row + 1
dws.Rows(dlr).Insert
BottomCell.Offset(-4).EntireRow.Delete
End If
dws.Range("B" & dlr).value = cell.value
dws.Range("C" & dlr).value = cell.Offset(0, 1).value
dws.Range("F" & dlr).value = cell.Offset(0, 3).value
dws.Range("G" & dlr).value = cell.Offset(0, 4).value
dws.Range("H" & dlr).value = cell.Offset(0, 5).value
dws.Range("I" & dlr).value = cell.Offset(0, 2).value
dws.Range("J" & dlr).value = cell.Offset(0, 6).value
End If
End If
Next cell Call MatchInvoiceData
'Err:
Application.ScreenUpdating = True
End Sub