Find matching range & replace adjacent cell value — Excel VBA

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



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:



and so on.



However, the User ID column is incorrect and needs to be updated with values from a separate sheet:



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. 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.



My VBA skills are poor (typical, I know) and this is as far as I have come:


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





Have you tried using vlookup() function? excelfunctions.net/Excel-Vlookup-Tutorial.html
– Alex
Oct 27 '14 at 22:19




2 Answers
2



As Alex stated, you don't need VBA, a VLookup will work just fine. You can concatenate both criteria into a third column on both sheets, then vlookup against that value.



Just make sure your concatenated column is at the left of the ID that you would like to retrieve.



Once you get your IDs updated, hard-code the formulas with a copy/pastespecial:values, and delete the concatenated column.





Thank you for the tip RE: VLookup. I have avoided spending much time with that function as I didn't think it suited my purposes. My objective here is to require as little manipulation of the data as possible as this will likely be repeated with other data sets as the need arises, hence the desire for VBA instead of a conventional formula w/ concatenation, etc.
– daniel
Oct 28 '14 at 14:09






I have, in the past, used VBA to create concatenation/vlookup columns. Excel is optimized for this and it is vastly more efficient than a loop. Of course it's your prerogative, but there's no reason you couldn't use both.
– n8.
Oct 28 '14 at 18:48



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





Thanks, PJ. I have corrected the typo (as well as one other), but that doesn't seem to have made any difference.
– daniel
Oct 28 '14 at 14:13





This line is also problematic: .Cells("C1").Value = Sheets("Values").Cells("C1") Regardless of the check criteria, you've got the same C1 updating from C1 on a different sheet over and over. Maybe it should be .Cells("C" & i).Value = Sheets("Values").Cells("C" & i)?
– n8.
Oct 28 '14 at 18:55






If anything, Range("C1") is the correct format there.
– peege
Oct 28 '14 at 20:31






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS