2016-06-28 8 views
0

は、私は、VLOOKUPコードを構築し、私は間違ってやっていることを理解するように見えることはできません。私は、実行時エラーを受け取る「9」:範囲のエラー・メッセージのうち添字。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式が動作していない理由として?

答えて

1

あなたは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 Sub