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

时间:2016-03-04 17:12:21

标签: excel excel-vba vba

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

我的代码示例如下。代码有效,但输出数据中存在间隙,因为众多Excel文件定义了可变列,并且并非所有列都有数据(定义时数据中的间隙)。

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

0 个答案:

没有答案