如何从一本书合并cellcontents到另一本书

我需要将工作簿1中不同列和单元格中的一些内容复制到工作簿2中。

这是一个例子:

Workbook1.Worksheets("sheet25").Range("B2:AA2") 

对于这些单元格中的每个值,它需要包含来自相同工作簿和工作表的2列:例如,B2值需要列A9:EndOfValue + B9:EndOfValue

这需要复制到workbook2.Sheet1("Sheet1").Range("A2:EndofValue")当代码进入工作簿1的C2列时,再次开始,以此类推工作簿2中的所有值到最后一个AA2)。

我会考虑for each循环中使用一个循环,但是我已经读过,这会在closures时留下大量的大文件(我的非常大,比如35000行,在复制26和100后会相乘)。

我想我有你的问题的解决scheme。 据我从你的描述收集,列A和列B的总和合并为一个单一的结果,是在新的工作簿的单元格A1返回,然后B1为接下来的两列等 – 我用26列,数字为1至35000。 我还假定你的数据是35000行,26行宽。 使用嵌套循环是我有的select,但我已经采用了一个dynamic数组,计算列A和B,B和C,C和D等的总和,直到它结束。

程序的逻辑分别使用xlRight和xlUp来build立数据的水平和垂直范围。 嵌套循环又有三个层次。 最外层决定新工作表中的连续单元格,其中输出总和,第二级用数据遍历每一列,最后一级将处理限制到最后一行数据。

每次处理列时,dynamic数组将增加到35000个单位。 每个数组项都是列中的活动单元格和其旁边的单元格的总和。 在每个循环结束时,在分析下一列之前,将数组和输出总和的值清零。

为了示例的目的,我使用了同一个文件的Sheet1和Sheet2,但是为了您的目的,它将像创build工作簿variables(egxworkbooks.add)并将其设置为输出目标一样简单。

不幸的是,这个程序还需要一段时间才能执行,但是我的机器确实和雷曼兄弟还有关系。

与代码一起,并且注释被集成:

 Sub summing_bigdata() Dim n As Double, a As Double, b As Double Dim Z As Integer Dim arr_s() As Variant Dim lstcl As Variant, lstcl2 As Variant 'establish the limits of the data lstcl = Range("A100000").End(xlUp).Row lstcl2 = Range("A1").End(xlToRight).Column 'establish successive output columns For Z = 1 To lstcl2 'looping through each column of data For a = 1 To lstcl2 'looping through each row of data For n = 1 To lstcl 'preserve the size of the array at each pass to prevent overwriting previous data ReDim Preserve arr_s(n) 'fill each array item with the sum of each cell in the column and the cell next to it arr_s(n) = Cells(n, a).Value + Cells(n, a).Offset(0, 1).Value Next 'add all the elements of the array into b b = Application.WorksheetFunction.Sum(arr_s) 'output b in the new sheet/file Sheets(2).Cells(1, 1).Offset(0, Z) = b Next 'reset the value of b and the array for the next column to be analyzed b = 0 Erase arr_s Next End Sub 

希望这有助于。

Wauw! 非常感谢您的时间和解释。 但是我很抱歉! 我显然犯了一个小错误。 第二本书的新专栏不是两列的总和。 我只需要在第二册的每一栏中复制这两列。 不过,我想我将能够使用你的循环来为我的贡献。 再次,非常感谢! 干杯。