Excel / VBA:无法打开工作簿

时间:2015-05-15 10:04:23

标签: excel vba excel-vba

Function getProductStatus(adr As Range) As String

Dim networkLocation As String, networkPath As String, fileReference As String, srcSheetRef As String, c_formula As String
Dim sheet_src As Worksheet, sheet As Worksheet, wb_src As Workbook, wb As Workbook
Dim sheetName_src As String, sheetName As String

networkLocation = ActiveWorkbook.Path
networkPath = networkLocation & "\Locations\"

sheetName_src = "Sheet1"
sheetName = "Sheet1"

Set wb_src = ThisWorkbook
Set sheet_src = wb_src.Sheets(sheetName_src)

fileReference = networkPath & region_eval & "\ProductList.xlsx"

Workbooks.Open Filename:=fileReference, ReadOnly:=True
Set wb = Workbooks("ProductList.xlsx")
Set sheet = wb.Sheets(sheetName)

c_formula = Application.WorksheetFunction.Index(sheet.Range("$K$2:$K$5000"), Application.WorksheetFunction.Match(sheet_src.Range("A" & adr.Row), sheet.Range("$A$2:$A$5000"), 0))

getProductStatus = c_formula
wb.Close

End Function

如果直接在单元格中输入,则以下公式有效:

=INDEX('\\..somePath\Locations\[ProductList.xlsx]Sheet1'!$K$2:$K$5000;MATCH('Sheet1'!A2;'\\..somePath\Locations\[ProductList.xlsx]Sheet1'!$A$2:$A$5000;0))

编辑:

之前的问题是#NAME错误,但已经解决了......只是演变成了更糟糕的错误。

此函数在某些时候确实按预期工作(在#NAME解析之后)。从那时到现在,除了关闭要求的文件之外什么都没有改变。现在,在运行该函数之前打开文件什么都不做。

我该如何调试?为什么Excel会停止打开文件?

编辑#2:显然,在不同的实例中打开文件,因为如果两个文件在同一个实例中打开,该功能就可以正常工作。所以真正的问题是,为什么我的代码不能成功打开文件“在它自己的”?

0 个答案:

没有答案