How to use vlookup and copy paste value on excel VBA

ExcelPupper

Board Regular
Joined
Mar 2, 2020
Messages
112
Office Version
  1. 2019
Platform
  1. Windows
Hello. To begin with, what I need is to have to lookup the needed data from another file.
I have here the screenshot of the result of lookup and paste values when I do it manually.

1592318451282.png


Kindly check my code below for corrections. Thanks!


VBA Code:
Sub LookUpData()
'
'
'
' Keyboard Shortcut: Ctrl+Shift+W
'
    
    Application.ScreenUpdating = False
    Dim MyFile As String, wb As Workbook

    MyFile = "C:\Users\ADMIN\Desktop\vba\SourceData.xlsm"
    
    
    Set wb = Workbooks.Open(MyFile)
    
    Workbooks("TestData").Sheets("Sheet1").Range("C2").Select
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC1,[SourceData.xlsm]Sheet1!C3:C5,2,FALSE)"
    Range("C2").Select
    Selection.Copy
    Range("D2").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC1,[SourceData.xlsm]Sheet1!C3:C5,3,FALSE)"
    Range("C2:D2").Select
    Selection.AutoFill Destination:=Range("C2:D4")
    Range("C2:D4").Select
    Application.CutCopyMode = False
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
End Sub
 

Attachments

  • 1592291595220.png
    1592291595220.png
    15.1 KB · Views: 78

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).

Forum statistics

Threads
1,214,954
Messages
6,122,462
Members
449,085
Latest member
ExcelError

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