我是VBA的新手,我有一个函数的代码如下:
'abc'
此代码的目的是使用Vlookup函数从另一个工作簿中获取某个数字,并将值返回到函数in
。
问题是我收到运行时错误9.当我单击Debug时,以下行突出显示:
Function checktype(ByVal modele As String)
Dim srchRange As Range
Dim book1 As Workbook
'Set some Workbook variables:
Set book1 = Workbooks("C:\Users\MZ\Desktop\EPI.xlsx")
Set srchRange = book1.Sheets(1).Range("A1:D800")
'This assumes that the Book2 is Open and you are on the desired active worksheet:
checktype = Application.WorksheetFunction.VLookup(modele, srchRange, 4, False)
End Function
答案 0 :(得分:1)
确保在excel的 同一实例 中打开工作簿,并将其引用如下:
Set book1 = Workbooks("C:\Users\MZ\Desktop\EPI.xlsx")
Set book1 = Workbooks("EPI.xlsx") 'specifying only the name is OK as well
或
Set book1 = Workbooks.Open("C:\Users\MZ\Desktop\EPI.xlsx")
同一实例中的2个Excel文件的示例: