• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Loop lookup copy paste

Pofski

Member
Hi,

I am looking for a vb code that would run through column A on the first worksheet, and looks up each reference number in column A of the second worksheet.
After this, if it would find the number in the second list, it should copy the line of information next to the reference number of the second worksheet to the first page.

Thanks
 
in my example, the numbers on the sheet "Data" should be completed with the information that can be found on the sheet "Reference".
 

Attachments

  • fruit finder.xlsx
    12.1 KB · Views: 5
Formula's (which i would personally prefer to use) are not an option in the situation that this file will be used in.
Would also recommend using the index/match lookup in place of vlookup for such things.
 
Copy the following into teh Data Code Module in VBA and run it with F5 or assign a Button to it

Code:
Sub Copy_Data()

Dim LR1 As Integer, LR2 As Integer

Sheets("Reference").Activate
LR2 = Range("A" & Rows.Count).End(xlUp).Row

Sheets("Data").Activate
LR1 = Range("A" & Rows.Count).End(xlUp).Row

Application.ScreenUpdating = False

For i = 2 To LR1
  FindString = CStr(Cells(i, 1))
  With Sheets("Reference").Range("A2:A" + CStr(LR2))
  Set Rng = .Find(What:=FindString, _
  After:=.Cells(.Cells.Count), _
  LookIn:=xlValues, _
  LookAt:=xlWhole, _
  SearchOrder:=xlByRows, _
  SearchDirection:=xlNext, _
  MatchCase:=False)
   
  If Not Rng Is Nothing Then
  Sheets("Reference").Range(Rng.Address).Offset(0, 1).Resize(, 3).Copy
  Range("B" + CStr(i)).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
  :=False, Transpose:=False
  Else
  Range("B" + CStr(i)) = "Not found"
  End If
  End With
   
Next i

Application.ScreenUpdating = True

End Sub
 
Marc,

thank you for your help as well.
I failed to check out your link, but i will still use it for potential future issue's.
 
As Marc L said above a Formula based solution would be much faster and could be locked down with protection if required
 
Back
Top