I am having two Sheets . Sheet1 : Last week, and sheet2 : This week.
I am looking for my ID in column A of sheet2 with sheet1, and if they match, I am copying the values from column M of sheet1 to column M of sheet2.
Due to some reason, the values that i dont find in sheet1 are getting filled as "0". I dont want this to occur, with my code. I just want the code to look for ID, if they match i want the value, else i dont want anything to be printed.
Could someone suggest Where i am going wrong ?
Sub lookup()
Dim tr As Long
Dim trsh As Long
tr = Sheets("ThisWeek").Cells(Rows.Count, "A").End(xlUp).Row
trsh = Sheets("ThisWeek").Cells(Rows.Count, "A").End(xlUp).Row
Sheets("ThisWeek").Range("M2:M" & tr).Formula = Application.WorksheetFunction.IfError(Application.VLookup(Sheets("ThisWeek").Range("A2:A" & trsh), Sheets("LastWeek").Range("$A:$P"), 13, 0), "")
End Sub
The code would be like this.
Instead of
try
Though your code can be modified using worksheet and range variable. And make sure you use correct
tr
andtrsh
.EDIT:
See image for reference.
Sheet LastWeek
Sheet ThisWeek
You may try something like this...
Please correct the sheet reference if required. Currently it assumes that the sheets are called as ThisWeek and LastWeek.