如何使用excel的VBA将多个文件中的表复制到单个文件中的一个表中

时间:2014-02-21 17:01:46

标签: excel vba excel-vba

我有多个excel文件,其中包含以下表格:

enter image description here

和此:

enter image description here

我想要一个包含这样一个表格的文件:

enter image description here

第三个文件是合并文件,其中所有值都在正确的位置。

聚苯乙烯。我必须将1000多个文件合并为1,所有列按名称排序 PPS我认为整个文件大约有1000列(每列是一种对象)和超过30000行

有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

由于您已设置了所有文件,因此可以将标题行复制到数组中,并将该值与其他工作表的第一行进行比较

Dim HeadArray, CopyArray
Dim RowCountA as Long, RowCountB as Long
Dim ColumnA as Long, Column2 as Long

RowCountA = Workbooks("All").Worksheets("Sheet1").Cells(Rows.Count,1).End(xlUp).Row
ColumnA = Workbooks("All").Worksheets("Sheet1").Cells(1,Columns.Count).End(xlLeft).Column
HeadArray = Workbooks("All").Worksheets("Sheet1").Cells(1).Resize(1,ColumnA)

'Will want to put the remaining code in a loop to accomodate the amount of files you are wanting
RowCountB = Workbooks("CopyBook").Worksheets("Sheet1").Cells(Rows.Count,1).End(xlUp).Row
ColumnB = Workbooks("CopyBook").Worksheets("Sheet1").Cells(1,Columns.Count).End(xlLeft).Column
CopyArray = Workbooks("CopyBook").Worksheets("Sheet1").Cells(1).Resize(RowCountB,ColumnB)

For x = 0 to ColumnA - 1
    For i = 0 to ColumnB - 1
        If CopyArray(0,i) = HeadArray(0,x) Then
            For y = 1 to RowCountB - 1
                Workbooks("All").Worksheets("Sheet1").Cells(RowCountA + y,x+1).Value = CopyArray(y - 1,i)
            Next y
        End if
    Next i
Next x

RowCountA = RowCountA + RowCountB