Excel VBA:如何从excel文件中打开和读取

编辑:user3561813添加"/"的build议之后,它现在读取第一个文件。 我有一个超出范围的错误消息"9" 。 它确实读取第一个文件。 最终,我试图打开每个文件,并阅读名称和年龄(这是一个testing不是真正的生产forms)。 并将这些值返回到我的主工作表中。

在这里输入图像描述

原来的问题

我试图读取文件夹中的excel表格的数百个,读取特定的单元格位置,并将其logging到我的testing工作表中。 我GOOGLE了这个教程,并试图写我的代码。 但是,当我执行获取文件夹function,select一个文件夹path,它不循环我有的Excel文件。 (或logging他们的名字)

 'Source: https://www.youtube.com/watch?v=7x1T4s8DVc0 Sub GettingFolder() Dim SelectedFolder As String With Application.FileDialog(msoFileDialogFolderPicker) .Title = "Select folder" .ButtonName = "Confirm" .InitialFileName = "U:\" If .Show = -1 Then 'ok clicked SelectedFolder = .SelectedItems(1) MsgBox SelectedFolder ' This is where I want to call my function LoopFiles (SelectedFolder) Else 'cancel clicked End If End With End Sub ' Source: http://www.excel-easy.com/vba/examples/files-in-a-directory.html Sub LoopFiles(path As String) Dim directory As String, fileName As String, sheet As Worksheet Dim i As Integer, j As Integer ' Avoid Screen flicker and improve performance Application.ScreenUpdating = False ' Fixed per suggestion below.. directory = path & "\" fileName = Dir(directory & "*.xl??") Do While fileName <> "" i = i + 1 j = 2 Cells(i, 1) = fileName Workbooks.Open (directory & fileName) For Each sheet In Workbooks(fileName).Worksheets Workbooks("Testing.xlsm").Worksheets(1).Cells(i, j).Value = sheet.Name j = j + 1 Next sheet Workbooks(fileName).Close fileName = Dir() Loop ' Reset the screen update setting Application.ScreenUpdating = True End Sub 

在您的代码中, pathvariables可能不包含尾部反斜杠。 这会导致LoopFiles(<>)例程中的以下代码不准确:

 directory = path fileName = Dir(directory & "*.xl??") 

文件c:\users\name\documentshello.xlsx如下所示: c:\users\name\documentshello.xlsx

尝试将上面的代码更改为:

 directory = path & "\" fileName = Dir(directory & "*.xl??") 

这是否解决了这个问题?

有趣的问题! 这应该为你做。 根据需要修改。

 Sub LoopAllExcelFilesInFolder() 'PURPOSE: To loop through all Excel files in a user specified folder and perform a set task on them 'SOURCE: www.TheSpreadsheetGuru.com Dim wb As Workbook Dim myPath As String Dim myFile As String Dim myExtension As String Dim FldrPicker As FileDialog 'Optimize Macro Speed Application.ScreenUpdating = False Application.EnableEvents = False Application.Calculation = xlCalculationManual 'Retrieve Target Folder Path From User Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker) With FldrPicker .Title = "Select A Target Folder" .AllowMultiSelect = False If .Show <> -1 Then GoTo NextCode myPath = .SelectedItems(1) & "\" End With 'In Case of Cancel NextCode: myPath = myPath If myPath = "" Then GoTo ResetSettings 'Target File Extension (must include wildcard "*") myExtension = "*.xlsx" 'Target Path with Ending Extention myFile = Dir(myPath & myExtension) Row = 1 'Loop through each Excel file in folder Do While myFile <> "" 'Set variable equal to opened workbook Set wb = Workbooks.Open(Filename:=myPath & myFile) 'Change First Worksheet's Background Fill Blue ThisWorkbook.Worksheets("Sheet1").Range("A" & Row).Value = Worksheets(1).Range("A1").Value Row = Row + 1 'Save and Close Workbook wb.Close SaveChanges:=True 'Get next file name myFile = Dir Loop 'Message Box when tasks are completed MsgBox "Task Complete!" ResetSettings: 'Reset Macro Optimization Settings Application.EnableEvents = True Application.Calculation = xlCalculationAutomatic Application.ScreenUpdating = True End Sub