VBA search & copy value from different WS

EMcK01

Board Regular
Joined
Jun 14, 2015
Messages
125
Hi,

I'm trying to search a range for a name, look in another sheet to match the name then copy an offset from this table to then bring back to my original sheet.

The first bit of searching the name to define what is being looked for is fine, however I'm unsure what I need to set the paste code as so it returns the found value.

This is where the code is falling down
Code:
Worksheets("export").ActiveCell.Offset(0, 1).PasteSpecial xlPasteValues

The full script is this

VBA Code:
Sub GetName
Dim lr As Long, rng As Range

    lr = Range("A" & Rows.Count).End(xlUp).Row
                      
      Set rng = Range("A2:A" & lr)
              For Each cell In rng
      
              If cell.Value <> "" Then

    rngY = cell

    Sheets("Ref").Select
    Columns("B:B").Select
    Selection.Find(What:=rngY, After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        matchCase:=False, SearchFormat:=False).Activate
      
    ActiveCell.Select

    ActiveCell.Offset(0, 5).Copy
    Worksheets("export").ActiveCell.Offset(0, 1).PasteSpecial xlPasteValues
        End If
    Next cell
End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
How about
VBA Code:
Sub EMcK()
   Dim Cl As Range
   Dim Dic As Object
   
   Set Dic = CreateObject("scripting.dictionary")
   Dic.comparemode = 1
   With Sheets("Ref")
      For Each Cl In .Range("B2", .Range("B" & Rows.Count).End(xlUp))
         If Cl <> "" Then Dic(Cl.Value) = Cl.Offset(, 5).Value
      Next Cl
   End With
   With Sheets("Export")
      For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
         If Dic.Exists(Cl.Value) Then Cl.Offset(, 1).Value = Dic(Cl.Value)
      Next Cl
   End With
End Sub
 
Upvote 0
Solution
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,199
Members
449,072
Latest member
DW Draft

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