简体   繁体   English

Excel中的Vlookup VBA代码,不在正确的单元格中复制数据(只需在我的代码中做一个小修复)

[英]Vlookup VBA Code in Excel, not copying data in the right cell (Need just a small fix in my code)

I need a little help with fixing the VBA code that I have written. 我需要一些帮助来修复我编写的VBA代码。 Question: 题:

I have an excel file with 3 columns on sheet1 ("Name", "ID" and "Name Data"): "Name Data" is a blank column. 我在sheet1上有3列的Excel文件(“名称”,“ ID”和“名称数据”):“名称数据”是空白列。

Name        ID      Name Data
Albert      4235 
John        4237 
Robert      4239 
Micky       4241 
Minnie      4243 
Donalid     4245 
Edited      4247 
Goofy       4249 

On sheet 2, there is a similar ID column with only one ID: eg: 在工作表2上,有一个类似的ID列,其中只有一个ID:例如:

ID
4243

I have the vlookup code to match the ID value on sheet2 (in this eg, that would be "4243") with ID value on sheet 1 and copy the name associated with that ID to "Name data" of sheet1. 我有vlookup代码,以将sheet2上的ID值(在本例中为“ 4243”)与工作表1上的ID值进行匹配,并将与该ID相关联的名称复制到sheet1的“名称数据”中。 The final result should look like this: 最终结果应如下所示:

Name       ID    Name Data
Albert    4235 
John      4237 
Robert    4239 
Micky     4241 
Minnie    4243     Minnie
Donalid   4245 
Edited    4247 
Goofy     4249

Issue: The issue with this code is that it copies the name onto first cell under "Name data". 问题:此代码的问题是将名称复制到“名称数据”下的第一个单元格中。 Eg: it does the following: 例如:它执行以下操作:

Name      ID      Name Data
Albert    4235     Minnie
John      4237 
Robert    4239 
Micky     4241 
Minnie    4243 
Donalid   4245 
Edited    4247 
Goofy     4249

The name "minnie" should get copied on 5th row under "Name data" and not on 1st row (excluding heading rows) 名称“ minnie”应该复制到“名称数据”下的第五行,而不是第一行(标题行除外)

Can anyone please suggest me what to fix in the code? 谁能建议我在代码中解决什么问题?

Thanks a ton! 万分感谢!

Here is the code: 这是代码:

Sub lookup()
x = 2
Set rng1 = Worksheets("Sheet1").Range("b4:b8")
Set rng2 = Worksheets("Sheet2").Range("A2:A2")
For Each c1 In rng1
For Each c2 In rng2
If c1.Value = c2.Value Then
Worksheets("sheet1").Cells(x, 3).Value = c2.Value
If c1.Value <> c2.Value Then
Worksheets("Sheet1").Cells(x, 3).Value = Worksheets("Sheet1").Cells(x, 3).Value
x = x + 1
End If
End If
Next
Next
End Sub

I know that it has something to do with this line: 我知道这与以下行有关:

Worksheets("sheet1").Cells(x, 3).Value = c2.Value

but I am not able to figure out what. 但我不知道是什么。

I finished up with the following code: 我完成了以下代码:

Sub lookup()

Set rng1 = Worksheets("sheet1").Range("B2:B19")
Set rng2 = Worksheets("sheet2").Range("A2:A2")

For Each c1 In rng1
    For Each c2 In rng2
        If c2.Value = c1.Value Then
            Worksheets("sheet1").Cells(c1.Row, 3).Value = Worksheets("sheet1").Cells(c1.Row, 1).Value
        End If
    Next
Next

End Sub

Your corrected file is shared: https://www.dropbox.com/s/0nsr1iuwnzt1s1x/VlookupVBA.xlsm 您更正的文件是共享的: https : //www.dropbox.com/s/0nsr1iuwnzt1s1x/VlookupVBA.xlsm

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

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