VBA search & copy value from different WS

EMcK01

Board Regular
Joined
Jun 14, 2015
Messages
110
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

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,236
Office Version
  1. 365
Platform
  1. Windows
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
 
Solution

EMcK01

Board Regular
Joined
Jun 14, 2015
Messages
110
Thanks Fluff, thats worked perfectly for me.

Again I appreciate the help.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,236
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 

Forum statistics

Threads
1,141,048
Messages
5,703,928
Members
421,321
Latest member
blusky4

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
Top