简体   繁体   English

Excel VBA-VLookup不复制数据

[英]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). 情况:我在sheet2(分数)中有数据,在sheet3(注释)中有数据。 In each sheet, column A has a ticket number. 在每个工作表中,列A都有票证编号。 The Scores sheet includes every possible ticket number; 分数表包含所有可能的票号; the Comments sheets only includes a subset of that total (approx 50%). 注释表仅包含该总数的一部分(约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. 分数表包含4个填充的列,而注释表包含2个。我需要查找以匹配现有的常见行(票号),然后在注释列B(2)中获取值并将其粘贴到E列(5)中。 sheet1的内容,这是CSAT摘要表,并且具有“分数”表中Col AD​​的内容。

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... 如果我删除了VLookup并仅放置了一个变量(例如“ A”),它会将A粘贴到正确的列中,即使在每一行中也是如此...

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. 显然,这是VLookup本身的事情。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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