当使用索引范围function时,VBA运行时错误1004“应用程序定义或对象定义的错误”

在我的macros中,我有以下代码:

Set v = Application.WorksheetFunction.Index(Worksheets("Sheet2").Range(Cells(2, 1), Cells(33, 3)), Application.WorksheetFunction.Match(1, (Worksheets("Sheet2").Range(Cells(2, 1), Cells(33, 1)) = Osheet.Cells(i, 2).Value) * (Worksheets("Sheet2").Range(Cells(2, 2), Cells(33, 2)) = Left(Osheet.Cells(5, 6).Value, 1)), 0), 3) 

数据最初在Sheet1中(这是代码中使用的Osheet),从中select了2个参数,使用Index Matchfunction从Sheet2中获取数据。

运行macros时,会显示运行时错误1004,应用程序定义或对象定义的错误。

任何帮助将不胜感激。

一个快速的猜测:

 with Worksheets("Sheet2") Set v = Application.WorksheetFunction.Index(.Range(.Cells(2, 1), .Cells(33, 3)), Application.WorksheetFunction.Match(1, (.Range(.Cells(2, 1), .Cells(33, 1)) = Osheet.Cells(i, 2).Value) * (.Range(.Cells(2, 2), .Cells(33, 2)) = Left(Osheet.Cells(5, 6).Value, 1)), 0), 3) end with 

诀窍是你应该给电子表格的参考单元/范围以及。 只是一个VBA的事情! 🙂

有关详细信息,请参阅Msdn https://msdn.microsoft.com/en-us/library/office/ff196273.aspx