从另一个工作簿Vlookup

时间:2018-05-25 13:54:20

标签: vba excel-vba excel

我正在尝试从另一个工作簿中查看数据。我在vlookup中使用文件名,我想让它变得动态。这是我的代码:

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("B6").Select

ThisWorkbook.Activate
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)

1 个答案:

答案 0 :(得分:0)

如下?

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

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