VBA:macros循环行和自动填充

编辑:如果我反而想自动填充这些单元格,下面的代码工作?

Sub BC_Edit() ' Define width and height of table Dim datasetWidth, datasetHeight As Integer ' Find values for width and height of table datasetWidth = Range("A1").End(xlToRight).Column datasetHeight = Range("A1").End(xlDown).Row ' Loop over each column For x = 1 To datasetWidth Set sourceRange = Cells(2,x) Set fillRange = Range(Cells(3, x), Cells(datasetHeight, x)) sourceRange.AutoFill Destination:=fillRange Next End Sub 

我正在处理几个非常大的数据集 – 每个大约3000行×4000列。 尽pipeExcel可能不是最好的工具,但我已经在数据上build立了大量的基础架构,并且不能移动到不同的框架。 我正在使用Excel 2007。

在一个特定的工作表中,当我尝试使用公式自动填充时,我已经通过复制和粘贴这个列到整个第二列B(3000 x 1)到其余的3000到3998select,或这个select的一部分,Excel给我一个内存/资源错误。 所以我想反过来遍历每一行和自动填充所有的列。 (换句话说,我想使用A2自动填充A3:A4000,B2自动填充B3:B4000,等等。)也许这将有助于解决内存问题。 我将如何去写一个macros来完成这个?

我会很感激这个问题上的任何build议,如果可能的话,也许有一些帮助适当的VBA代码。

下面是一个非常基本的macros示例,用于将列设置为低于2的公式列。最好是将其附加到button或类似的东西上,而不是每次打开表单时都运行。

 Sub Button_Click() ' Define width and height of table Dim datasetWidth, datasetHeight As Integer ' Find values for width and height of table datasetWidth = Range("A1").End(xlToRight).Column datasetHeight = Range("A1").End(xlDown).Row ' Loop over each column For x = 1 To datasetWidth ' From row 3 to the height of data, set the formula of the cells to ' the formula contained in row 2 of that column Range(Cells(3, x), Cells(datasetHeight, x)).Formula = Cells(2, x).Formula Next End Sub