Home » excel » vba – Vlookup from another workbook

vba – Vlookup from another workbook

Posted by: admin May 14, 2020 Leave a comment


I am trying to vlookup data from another workbook. I am pulling with file name in vlookup and I want to make it dynamic. Here is my code:

Dim INPUT_PATH As String
Dim WBK As Workbook
Application.DisplayAlerts = True
Application.ScreenUpdating = True

INPUT_PATH = Sheet3.TextBox1.Text

Set WBK = Workbooks.Open(INPUT_PATH)
WBK.Windows(1).Visible = True

ActiveSheet.Unprotect Password:="nch"

Range("D10").Formula = "=VLOOKUP(G8,'[Rate.xlsx]Asset Depreciation'!$C:$J,2,0)
Range("G10").Formula = "=VLOOKUP(G8,'[Rate.xlsx]Asset Depreciation'!$C:$J,3,0)
Range("D12").Formula = "=VLOOKUP(G8,'[Rate.xlsx]Asset Depreciation'!$C:$J,4,0)
Range("G12").Formula = "=VLOOKUP(G8,'[Rate.xlsx]Asset Depreciation'!$C:$J,5,0)
Range("D14").Formula = "=VLOOKUP(G8,'[Rate.xlsx]Asset Depreciation'!$C:$J,6,0)
Range("G14").Formula = "=VLOOKUP(G8,'[Rate.xlsx]Asset Depreciation'!$C:$J,7,0)
Range("F16").Formula = "=VLOOKUP(G8,'[Rate.xlsx]Asset Depreciation'!$C:$J,8,0)
How to&Answers:

As follows?

Dim fileName As String, sheetName As String
fileName = "Rate.xlsx"
sheetName = "Asset Depreciation"

Range("D10").Formula = "=VLOOKUP(G8,'[" & fileName & "]" & sheetName & "'!$C:$J,2,0)"