I have a VBA code that runs a vlookup and than copies and pastes any row that returns #N/A to a different sheet and than deletes that row from the original worksheet. The problem I am having is that the code is entering the vlookup into columns that already have data so the data that was there is no longer on the sheet at all. My code is:
Code:
Sub ChequeRemove()
Dim wb As Workbook, oWS As Worksheet, nWS As Worksheet
Dim i As Long, LastRow As Long, LastCol As Long, nLastRow As Long
Set wb = ActiveWorkbook
Set oWS = wb.Sheets("Outstanding ST Loans")
Set nWS = wb.Sheets("New")
nLastRow = nWS.Cells.Find(What:="*", After:=[A1], SearchDirection:=xlPrevious).Row
LastRow = oWS.Cells.Find(What:="*", After:=[A1], SearchDirection:=xlPrevious).Row
LastCol = oWS.Cells.Find(What:="*", After:=[A1], SearchDirection:=xlPrevious).Column
oWS.Range(Cells(1, LastCol + 1), Cells(LastRow, LastCol + 1)).FormulaR1C1 = "=VLOOKUP(RC[" & 11 - (LastCol + 1) & "],'" & nWS.Name & "'!R1C4:R" & nLastRow & "C4,1,FALSE)"
For i = LastRow To 1 Step -1
If oWS.Cells(i, (LastCol + 1)).Text = "#N/A" Then If oWS.Cells(i, (LastCol + 1)).Text = "#N/A" Then Rows(i).Copy Destination:=Sheets("Removed").Range("A" & Rows.Count).End(xlUp).Offset(1)
If oWS.Cells(i, (LastCol + 1)).Text = "#N/A" Then Rows(i).Delete
Next i
End Sub