我构建了一个vlookup代码,但似乎无法理解我做错了什么。我收到一个运行时错误'9':下标的范围错误信息。
Cells(lrow + 1, 2) = Application.WorksheetFunction.vlookup( _
Corp.Sheets("Sheet3").Range("$B$1"), _
Workbooks("S:\_Shared Files MTL\Corporate Spreads\Weekly Sheets\" & _
myvalue & "_weekly sheet.xls"). _
Sheets("Pricing Sheet").Range("$B$18:$L$232"), 5, False)我正在尝试将数据从另一个工作簿中提取到当前工作簿(Dim Corp)中。
另一个工作簿具有动态名称,每周更改日期(myvalue)。
我要求用户使用消息框输入日期:
myvalue = InputBox("Insert date of file to upload in format yy_mm_dd", "User date", Format(Now(), "yy_mm_dd"))对于为什么vlookup公式不起作用有什么想法吗?
发布于 2016-06-29 00:31:08
无法在Excel中未打开的工作簿上运行vlookup。您首先需要打开工作簿。
未经测试:
Sub Tester()
Const MY_PATH As String = "S:\_Shared Files MTL\Corporate Spreads\Weekly Sheets\"
Dim myValue, fName As String, wb As Workbook, v, sht As Worksheet
Set sht = ActiveSheet
myValue = InputBox("Insert date of file to upload in format yy_mm_dd", _
"User date", Format(Now(), "yy_mm_dd"))
fName = myValue & "_weekly sheet.xls"
If Dir(MY_PATH & fName, vbNormal) <> "" Then
Set wb = Workbooks.Open(MY_PATH & fName, ReadOnly:=True)
v = Application.VLookup(Corp.Sheets("Sheet3").Range("$B$1"), _
wb.Sheets("Pricing Sheet").Range("$B$18:$L$232"), 5, False)
sht.Cells(lrow + 1, 2) = IIf(IsError(v), "No match!", v)
wb.Close False
Else
MsgBox "no matching file found!"
End If
End Subhttps://stackoverflow.com/questions/38085870
复制相似问题