2010 Excel VBA,如何定义从一个文件中的多个文件复制数据的固定列和variables行(打开一个文件夹中的所有Excel文件)。

Excel VBA如何定义将固定列和variables行(数组)从多个文件复制到一个文件(打开一个文件夹中的所有Excel文件)。

我的代码示例如下。 该代码工作,但在输出数据中有差距,因为众多的Excel文件中定义了variables列,并不是所有这些列都有数据(定义时数据中有间隙)。

列A和C总是在每个文件中都有数据。
列B和D在某些单元格中可能没有数据。 E栏和F栏可能定义也可能不定义,可能包含也可能不包含数据。

我只需要复制固定列A到D并复制到可变长度的行。

谢谢。

Sub LoopThroughFilesInFolder() Dim mainwb As Workbook Dim wb As Workbook Dim i As Integer Set mainwb = ThisWorkbook Set FileSystemObj = CreateObject("Scripting.FileSystemObject") 'use path to the folder Set FOlderObj = FileSystemObj.GetFOlder("E:\1-Chris Micha Master\DDTs all 2015 April 8\DDT Excel files") 'loop through the files For Each fileobj In FOlderObj.Files If fileobj.Name <> "OpenAllExcelFilesInAFolder.xlsm" And fileobj.Name <> "~$OpenAllExcelFilesInAFolder.xlsm" And (FileSystemObj.GetExtensionName(fileobj.Path) = "xls") Then Application.DisplayAlerts = False Set wb = Workbooks.Open(fileobj.Path) 'copy the results from the just opened workbook wb.Worksheets("DTCs").Select lastcell = Range("A1:XFD1048576").SpecialCells(xlCellTypeLastCell).Address Range("A6:" & lastcell).Select Selection.Copy 'go to the mainworkbook and paste data mainwb.Activate Sheets("Sheet1").Select If Range("a6").Value = "" Then Range("a1").Select Else Range("a6").End(xlDown).Offset(1, 0).Select End If ActiveSheet.Paste Kwb.Activate wb.Save wb.Close mainwb.Activate End If Next fileobj End Sub