需要从目录中的所有文件中提取数据

可能重复:
Excel – VBA问题。 需要从目录中的所有excel文件访问数据而不打开文件

所以我需要从一个目录中的多个文件中提取数据,并将其粘贴到一个Excel文件中,而无需打开这些文件。 有人很高兴给我提供关于如何做一个单一的文件的代码,现在我只需要弄清楚如何做到这一点在目录中的所有文件。 这个代码是针对单个单元格的,我需要它的范围。 这不是一个问题,但我只是想我会提及它。

Dim rngDestinationCell As Range Dim rngSourceCell As Range Dim xlsPath As String Dim xlsFilename As String Dim sourceSheetName As String Set rngDestinationCell = Cells(3,1) ' or Range("A3") Set rngSourceCell = Cells(3,1) xlsPath = "C:\MyPath" xlsFilename = "MyBook.xls" sourceSheetName = "Sheet1" rngDestinationCell.Formula = "=" _ & "'" & xlsPath & "\[" & xlsFilename & "]" & sourceSheetName & "'!" _ & rngSourceCell.Address 

所以我假设我必须做一些循环来运行所有的文件,但我不知道该怎么做。 如果有人能帮助我,我真的很感激。

  1. 你可以在VBA中做到这一点。 而在这种情况下,你可以说是应该的。

  2. 750(甚至1000)是不是过度。 不要担心错误的经济,根据你以前的职位 。

  3. 你的基本algorithm是:

    1)确定需要阅读的.xls(x)文件2)在循环中提取所需的信息

  4. 有许多不同的方法来完成1)和2)。

    例如:

 Dim wbList() As String, wbCount As Integer, i As Integer FolderName = "C:\Foldername" ' create list of workbooks in foldername wbName = Dir(FolderName & "\" & "*.xls") While wbName "" -- get info, per any of the links you've already been given -- Wend 

虽然我认为这篇文章应该已经在第一篇文章中完成了,但您可以使用下面的代码,它是从我提供的早期链接中导出的,可以从您可以指定的文件夹中合并每个表单的第2行,名为“loging form” :\ temp到你的path)

该代码将查看.xls,以便在Excel 2003文件或Excel 2007/10文件上运行。 Dir适用于所有版本。 代码将跳过不包含名为“loging form”的工作表的任何工作簿

最后,将返回的行合并到托pipe代码的工作簿的新工作表(作为值)中,每次代码运行时都会创build一个新工作表

 Sub ConFiles() Dim Wbname As String Dim Wb As Workbook Dim ws As Worksheet Dim ws1 As Worksheet Dim lngCalc As Long Dim lngrow As Long With Application .ScreenUpdating = False .EnableEvents = False lngCalc = .CalculationState .Calculation = xlCalculationManual End With Set ws1 = ThisWorkbook.Sheets.Add 'change folder path here FolderName = "C:\temp" Wbname = Dir(FolderName & "\" & "*.xls*") 'ThisWorkbook.Sheets(1).UsedRange.ClearContents Do While Len(Wbname) > 0 Set Wb = Workbooks.Open(FolderName & "\" & Wbname) Set ws = Nothing On Error Resume Next 'change sheet name here Set ws = Wb.Sheets("loging form") On Error GoTo 0 If Not ws Is Nothing Then lngrow = lngrow + 1 ws.Rows(2).Copy ws1.Cells(lngrow, "A") End If Wb.Close False Wbname = Dir Loop With Application .ScreenUpdating = True .EnableEvents = True .Calculation = lngCalc End With End Sub 

你可以像这样做一个文件search:

 ' Find all .xls files in a directory Dim ff As FoundFiles With Application.FileSearch .LookIn = "C:\MyPath\" .FileType = msoFileTypeExcelWorkbooks .Execute Set ff = .FoundFiles End With ' ff is now a collection of full paths to all excel files in dir ' But you need the filename separate from the folder path... ' FileSystemObject is a convenient tool to manipulate filenames (and more...) ' Before using it, you need to set a reference to it: ' Tools > References > set tickmark next to Microsoft Scripting Runtime Dim FSO As Scripting.FileSystemObject Set FSO = New FileSystemObject Dim i As Integer For i = 1 To ff.Count xlsFilename = FSO.GetFileName(ff.Item(i)) ' Do your stuff here... Next i 

作为Application.FileSearch的替代方法,您可以尝试使用Dir函数。 看看VBA帮助Dir