Search for Matching Cell and Copy a Value

chartzell

New Member
Joined
Mar 8, 2013
Messages
11
I need what seems to be a fairly straightforward and simple macro, but I haven't written VBA in over a year and keep drawing blanks. Here's what I'm trying to do in pseudo-code.

Code:
lRow = 2

Do
  Search Sheet2.ColumnA for value matching Sheet1.ColumnA.Row(lRow)

  If matching value is found Then
    copy corresponding value in Sheet2.ColumnB to Sheet1.ColumnD.Row(lRow)

  lRow++

Loop Until Len(Cells(lRow, A)) = 0

Any snippets of code or links to appropriate documentation are appreciated. I'll be browsing documentation and seeing if I can figure this out on my own in the mean time.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
You could do this without VBA...

In Sheet1, column D

=INDEX(Sheet2!B:B,MATCH(A2,Sheet2!A:A,0))
 
Upvote 0
You shouldn't need to loop unless I'm missing something.

Code:
Sub matchCell()

    Dim startRow As Integer
    Dim endRow As Long
    
    Dim searchArea As Range


    Set searchArea = Sheets(2).Range("A:B")
    
    startRow = 2
    endRow = Sheets(1).Cells(Rows.Count, "A").End(xlUp).Row
    
    With Range(Cells(startRow, 4), Cells(endRow, 4))
    
        .FormulaR1C1 = "=VLOOKUP(RC1," & Sheets(2).Name & "!C1:C2,2,0)"
        .Value = .Value
    
    End With
    


End Sub
 
Upvote 0

Forum statistics

Threads
1,214,650
Messages
6,120,734
Members
448,987
Latest member
marion_davis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top