简体   繁体   English

查找匹配范围并替换相邻的单元格值— Excel VBA

[英]Find matching range & replace adjacent cell value — Excel VBA

I have two data sets that I need to compare--one that contains my master data set, and a second with corrected values for one variable in that data set. 我有两个需要比较的数据集-一个包含主数据集,另一个包含该数据集中一个变量的校正值。

For instance, 例如,

Data Set: 资料集:

FirstName1 | LastName1 | UserID1 | etc1 | etc1  
FirstName2 | LastName2 | UserID2 | etc2 | etc2  
FirstName3 | LastName3 | UserID3 | etc3 | etc3

and so on. 等等。

However, the User ID column is incorrect and needs to be updated with values from a separate sheet: 但是,“用户ID”列不正确,需要使用单独工作表中的值进行更新:

FirstName1 | LastName1 | **Corrected**UserID1  
FirstName2 | LastName2 | **Corrected**UserID2  
FirstName3 | LastName3 | **Corrected**UserID3

and so on. 等等。

My objective is to find a match using the first two cells as criteria, then if a match is found, replace the User ID in the first sheet accordingly. 我的目标是使用前两个单元格作为条件来查找匹配项,然后,如果找到匹配项,请相应地替换第一张工作表中的用户ID。 Unfortunately, the original data set is much smaller (~3k rows) than the sheet where the corrections are coming from (~30k rows), so a simpler solution has not yet been identified. 不幸的是,原始数据集(约3k行)比要进行校正的工作表(约3万行)要小得多,因此尚未找到更简单的解决方案。

My VBA skills are poor (typical, I know) and this is as far as I have come: 我的VBA技能很差(我知道是典型的),而据我所知:

Sub FindReplace()
  Dim lr As Long, i As Long
  With Sheets("Data Set")
    lr = .Range("A:B").End(xlUp).Row
    For i = lr To 1
        If IsNumeric(Application.Match(.Range("A1:B1").Value, Sheets("Values").Range("A1:B1").Value, 0)) Then 
          .Cells("C1").Value = Sheets("Values").Cells("C1")
        End If
    Next i
  End With
End Sub

However, this doesn't do anything and I'm a bit stumped (and over my head). 但是,这无能为力,我有些困惑(头顶)。 Ideally, I would appreciate some additional explanation of why the above doesn't work so that I might learn from my mistakes, but any help at all is infinitely appreciated. 理想情况下,我将对为什么上述方法不起作用的一些其他解释表示赞赏,以便我可以从自己的错误中吸取教训,但是对您的任何帮助都是无限的。

Many thanks for the assistance! 非常感谢您的协助!

+ds + ds

Without getting into building and testing this, I can see that you have a typo. 在不进行构建和测试的情况下,我可以看到您有错字。

Sub FindReplace()
Dim lr As Long, i As Long
With Sheets("Data Set")
    lr = .Range("A:B").End(xlUp).Row
    **For i = lr To 1**
        If IsNumeric(Application.Match(.Range("A1:B1").Value, Sheets("Values").Range("A1:B1").Value, 0)) Then .Cells("C1").Value = Sheets("Values").Cells("C1")
    Next i
End With
End Sub

Should be 应该

    For i = 1 To lr

As Alex stated, you don't need VBA, a VLookup will work just fine. 如Alex所述,您不需要VBA,VLookup可以正常工作。 You can concatenate both criteria into a third column on both sheets, then vlookup against that value. 您可以将两个条件连接到两张纸的第三列中,然后针对该值进行vlookup。

Just make sure your concatenated column is at the left of the ID that you would like to retrieve. 只要确保您的串联列在您要检索的ID的左侧即可。

FirstName1LastName1 |FirstName1 | LastName1 | CorrectedUserID1
FirstName2LastName2 |FirstName2 | LastName2 | CorrectedUserID2
FirstName3LastName3 |FirstName3 | LastName3 | CorrectedUserID3

Once you get your IDs updated, hard-code the formulas with a copy/pastespecial:values, and delete the concatenated column. 更新ID后,请使用copy / pastespecial:values对公式进行硬编码,然后删除串联的列。

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

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