Vlookup:对象不支持这个属性或方法

2015年9月19日更新:为了解决所提出的问题,我将该分为三部分。

  1. 将文件从一个地方复制到另一个地方
  2. 将(使用save-as方法)xls转换为xlsx。 该文件可以打开。
  3. 查看该值并将其打印在指定的单元格上。

我设法完成了前两个任务,第二个任务是帮助: 使用VBA将.xls批量转换为.xlsx,而无需打开工作簿

但是,popup的错误:运行时下标超出范围


先前:

嗨,这是我第一次问一个问题,我尽了最大的努力去坚持提供的技巧和指导。 注意:假定macros在a.xlsx中执行

我写了这个macros:

  1. 从pathA复制文件(abc.xls)
  2. 将文件(abc.xls)粘贴到pathB
  3. 将文件从abc.xls重命名为abc.xlsx
  4. 执行vlookup查找abc.xlsx中的值并返回a.xlsx指定单元格中的值。

Sub CopyFile() Dim FSO Dim sFile As String Dim sSFolder As String Dim sDFolder As String Dim i As Integer Dim wbk As Workbook ' Get the number of times to loop from Cell D2 NumLoop = Cells(2, "D") ' Establish "For" loop to loop "NumLoop" number of times. For i = 1 To NumLoop ' This is the file name, examle "LBF-010114.xls" sFile = Sheets("Data Pointer").Cells(i + 2, "AG") 'This is the source file's path/location, example" D:\users\destop\A\" sSFolder = Sheets("Data Pointer").Cells(i + 2, "AD") 'this is the destination file's path, example" D:\users\destop\B\" sDFolder = Sheets("Data Pointer").Cells(i + 2, "AF") Set FSO = CreateObject("Scripting.FileSystemObject") If Not FSO.FileExists(sSFolder & sFile) Then ElseIf Not FSO.FileExists(sDFolder & sFile) Then FSO.CopyFile (sSFolder & sFile), sDFolder, True 'Just a check point to see if the code executed until this point Cells(i + 5, "E") = "File Exist" Else MsgBox "Specified File Already Exists In The Destination Folder", vbExclamation, "File Already Exists" End If Next End Sub Sub ConvertAllFile() 'I refer to https://stackoverflow.com/questions/29167539/batch-convert-xls-to-xlsx-with-vba-without-opening-the-workbooks 'All credits go to them Dim strCurrentFileExt As String Dim strNewFileExt As String Dim objFSO As Object Dim objFolder As Object Dim objFile As Object Dim xlFile As Workbook Dim strNewName As String Dim strFolderPath As String strCurrentFileExt = ".xls" strNewFileExt = ".xlsx" strFolderPath = "D:\Users\COM_GSY.APLIFEISGREAT\Desktop\LBF Fund\" Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFolder = objFSO.getfolder(strFolderPath) For Each objFile In objFolder.Files strNewName = objFile.Name If Right(strNewName, Len(strCurrentFileExt)) = strCurrentFileExt Then Set xlFile = Workbooks.Open(objFile.Path, , True) strNewName = Replace(strNewName, strCurrentFileExt, strNewFileExt) Application.DisplayAlerts = False Select Case strNewFileExt Case ".xlsx" xlFile.SaveAs strFolderPath & strNewName, XlFileFormat.xlOpenXMLWorkbook Case ".xlsm" xlFile.SaveAs strFolderPath & strNewName, XlFileFormat.xlOpenXMLWorkbookMacroEnabled End Select xlFile.Close Application.DisplayAlerts = True End If Next objFile ClearMemory: strCurrentFileExt = vbNullString strNewFileExt = vbNullString Set objFSO = Nothing Set objFolder = Nothing Set objFile = Nothing Set xlFile = Nothing strNewName = vbNullString strFolderPath = vbNullString End Sub Sub PrintNAV() Dim i As Integer Dim FilePath As String Dim sFile As String Dim FSO NumLoop = Cells(2, "D") For i = 1 To NumLoop 'Get the file name sFile = Sheets("Data Pointer").Cells(i + 2, "AG") 'Set the file path FilePath = "D:\Users\COM_GSY.APLIFEISGREAT\Desktop\LBF Fund\" Set FSO = CreateObject("Scripting.FileSystemObject") If FSO.FileExists(FilePath & sFile) Then Cells(i + 5, "B") = Application.WorksheetFunction.vLookUp(Sheets("Data Pointer").Cells(i + 2, "N"), Workbooks(FilePath & sFile).Sheets("Sheet1").Range("A1:L120"), 12, False) End If Next i End Sub 

我在这里查了一下论坛上的一些评论,据说可以执行closuresexcel工作簿的vlookup,只要是xlsx格式就可以了。 不知道这是多么真实。

再次,我将不胜感激任何评论。

工作簿集合是“当前在Microsoft Excel应用程序中打开的所有工作簿对象的集合”。 。 它不包括硬盘上的工作簿,也不打开。

如果您完全确定要访问已closures的工作簿,请将该公式写入工作表单元格,以照顾未打开的工作簿。 通过在打开的工作表上评估本机工作表公式,您不必打开外部工作簿。 您可以通过将其恢复为其返回值来删除该公式。

 dim extVLOOKUP as string If FSO.FileExists(FilePath & sFile) Then With Sheets("Data Pointer") extVLOOKUP = "=VLOOKUP(""" & .Cells(i + 2, "N") & """, '" & FilePath & "[" & sFile & "]Sheet1'!$A:$L, 12, FALSE)" 'Debug.Print extVLOOKUP '<~~ uncomment to check the formula '.Cells(i + 5, "B") = Application.Evaluate(extVLOOKUP) .Cells(i + 5, "B").Formula = extVLOOKUP .Cells(i + 5, "B") = .Cells(i + 5, "B").Value End With End If 

我已经包装了Sheets(“Data Pointer”)中的值,单引号中的单元格(i + 2,“N”)就像文本值一样,必须用工作表公式中的引号括起来。 如果它是一个数字,报价可以(也可能应该)被删除。