简体   繁体   中英

Excel VBA - VLookup not copying data

I have done my best to read every thread imaginable to rewrite my overall code to get this to work.

Situation: I have data in sheet2 (Scores) and data in sheet3 (Comments). In each sheet, column A has a ticket number. The Scores sheet includes every possible ticket number; the Comments sheets only includes a subset of that total (approx 50%). The Scores sheet includes 4 populated columns while the Comments sheet has 2. I need the lookup to match the common existing rows (ticket numbers), and then take the value in Comments column B (2) and paste it in Column E (5) of sheet1, which is the CSAT Summary sheet, and has the contents of Col AD from the Scores sheet.

Below is the current code. If I remove the VLookup and just put a variable (such as "A"), it does paste A in the correct column, albeit on every single line...

scores.UsedRange.Columns.Copy
csatSum.Range("A1").Insert
csatSum.Activate
Set rng = csatSum.Range("A2:A" & lastRow)
rngLastRow = Comments.Range("B2:B" & lastRow)
For i = 2 To lastRow
    On Error Resume Next
    csatSum.Cells(i, 5) = WorksheetFunction.VLookup(rng, Comments.Range(rngLastRow), 2, False)
    On Error GoTo 0
Next i

Not sure what I am missing. Obviously it is something in the VLookup itself. But I am at a loss.

I think it should be:

scores.UsedRange.Columns.Copy
csatSum.Range("A1").Insert
'csatSum.Activate
'Set rng = csatSum.Range("A2:A" & lastRow)
Set rngLastRow = Comments.Range("A2:B" & lastRow)
For i = 2 To lastRow
    On Error Resume Next
    csatSum.Cells(i, 5) = WorksheetFunction.VLookup(csatSum.Cells(i, 1), rngLastRow, 2, False)
    On Error GoTo 0
Next i

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM