文件打开帮助 – Excelmacros

我对脚本相对比较陌生,因此来到这里寻求一些帮助,帮助我build立一个优秀的macros。 我目前正在开发一个excel文件来加速数据捕获和validation。 我无法绕过如何去获得实际的数据。

我目前有一个包含所有文件夹和Excel文件的驱动器:

Y:\Audit\Accounting_Data\XXXXX_Company_Names\07 Jul 2013\XXXXX.xls

对我来说,第一个问题是每个公司都以不同的文件命名约定发送一个文件。 有些具有数字值的所有date,而另一些具有字母数字数据(而不是以相同顺序,即一些是DD / MM / YYYY,而另一些具有MMMM / DD / YYYY)。 我不能修改文件命名约定,因为它们被分享给其他服务,最重要的是我只能读取这些文件。

第二个问题是,每个公司都不会在同一天生成文件。 有些产生审计文件每天,有些只在平日(周末的那些创build,并在星期一上午发送给我)>>我正在考虑使用object.fso获得最后10个文件的date.created标准和有没有find更多的文件时,excel停止search/ /以前提到的问题是,有些文件是在同一天创build的。

另外,我试图实现一个循环function(当它打到一个空白单元格时停止),因为公司可以从sheet1中定义的列表中添加或删除。

我想要的是一种让excel进入当前月份文件夹并打开10个以前的excel文件并复制当前工作表中特定单元格的粘贴数据的方法。

这是我现在提出的:

单元格A4:A12 =文件path(即Y:\ Audit \ Accounting_Data \ XXXXX_Company_Names)

 var1=file path var2=month (numeric) var3=month var4=year Range (a4:a50) Do Loop till blank cell in Range (a4:a50) If cell is not blank then goto "var1\var2+var3+var4\" Excel is now in Y:\Audit\Accounting_Data\XXXXX_Company_Names\07 Jul 2013\ (hopefully) 

如何告诉excel打开与当前date相关的以前的10个excel文件,如果没有find,就停止

  Copy Data Paste Data Move to next line Repeat the Open 10 previous files / Copy / Paste else when cell is blank stop 

像这样的东西应该为你工作。 它应该通过数组中的每个文件夹并获取存储在文件夹中的所有文件,按datesorting,打开最多10个文件,并将每个文件复制到工作表上。

在这个例子中,我使用“Sheet1”作为工作表将所有的数据复制到,我用一个名为“DateList”的表来存储所有的文件path和创builddate。

 Sub Example() Dim DirList() As Variant Dim Path As Variant Dim fso As Object Dim dt As Date Dim CurrFile As String Dim RowOffset As Long DirList = Array("C:\Test\", "C:\Test - Copy\") 'Create list of folders to search Set fso = CreateObject("Scripting.FileSystemObject") 'Create file system object Sheets("DateList").Cells.Delete Sheets("DateList").Range("A1").Value = "Path" Sheets("DateLIst").Range("B1").Value = "Date Created" 'Loop through every directory in the list For Each Path In DirList() CurrFile = Dir(Path) 'For each file in the current directory Do While CurrFile <> "" 'Get the files date created dt = fso.GetFile(Path & CurrFile).DateCreated 'Add the file data to a "DateList" Sheets("DateList").Cells(Sheets("DateList").UsedRange.Rows.Count + 1, 1).Value = Path & CurrFile Sheets("DateList").Cells(Sheets("DateList").UsedRange.Rows.Count, 2).Value = Format(dt, "yyyymmdd") CurrFile = Dir Loop Sheets("DateList").Select 'Sort Files With ActiveWorkbook.Worksheets("DateList").Sort .SortFields.Clear .SortFields.Add Key:=Range("B1"), _ SortOn:=xlSortOnValues, _ Order:=xlDescending, _ DataOption:=xlSortNormal .SetRange Sheets("DateList").UsedRange .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With Sheets("Sheet1").Select 'Get up to 10 files For i = 2 To 11 If Sheets("DateList").Cells(i, 1).Value = "" Then Exit For End If 'Open the file, copy it to the bottom of the data on Sheet1 '***NOTE*** THIS ASSUMES SHEET1 STARTS OFF BLANK Workbooks.Open Sheets("DateList").Cells(i, 1).Value ActiveSheet.UsedRange.Copy Destination:=ThisWorkbook.Sheets("Sheet1").Cells(1 + RowOffset, 1) RowOffset = RowOffset + ActiveSheet.UsedRange.Rows.Count ActiveWorkbook.Close Next Sheets("DateList").Select Range(Cells(2, 1), Cells(ActiveSheet.UsedRange.Rows.Count, 2)).Delete Sheets("Sheet1").Select Next End Sub