Excel VBA:是否将自动填充区域切割成一半或四分之一可以减less大面积的计算时间?

我试图在excel VBA中编写一个效率程序,基本上这个程序通过一个前瞻性工作表search三个匹配标准,如果匹配,则从第四个列中拉取。 这是通过使用CountIf函数完成的。 每当三列匹配时,第四列中的值就被添加。 这是使用SumIf函数完成的。 我自动填充这个CountIf(SumIf())函数在100 +行,5000 +列区域。

到目前为止,代码只花了大约45分钟来计算。 我想知道如果我把这个区域分成4个单独的计算或者重新编写我的等式,我可以减less这个运行时间? 正如你在我的代码末尾看到的那样,我完成另一个计算来显示每一列的平均值,这就增加了更多的时间。

这是我的代码:

Sheets("8MO Raw Data").Select Range("A1048576").End(xlUp).Select EightMOLastRow = ActiveCell.Row Sheets("Shop Average Calc").Select Range("A1048576").End(xlUp).Select ShopAverageLastRow = ActiveCell.Row Range("B3") = "=IF(COUNTIFS('8MO Raw Data'!$D$2:$D$" & EightMOLastRow & ",B$1,'8MO Raw Data'!$F$2:$F$" & EightMOLastRow & ",B$2,'8MO Raw Data'!$B$2:$B$" & EightMOLastRow & ",$A3)=0,0,((SUMIFS('8MO Raw Data'!$Q$2:$Q$" & EightMOLastRow & ",'8MO Raw Data'!$D$2:$D$" & EightMOLastRow & ",B$1,'8MO Raw Data'!$F$2:$F$" & EightMOLastRow & ",B$2,'8MO Raw Data'!$B$2:$B$" & EightMOLastRow & ",$A3))))" Range("B2").End(xlToRight).Select ShopAverageLastColumn = ActiveCell.Column Range("B3").Select Selection.AutoFill Destination:=Range(Cells(3, 2), Cells(3, ShopAverageLastColumn)), Type:=xlFillDefault Range(Cells(3, 2), Cells(3, ShopAverageLastColumn)).Select Selection.AutoFill Destination:=Range(Cells(3, 2), Cells(ShopAverageLastRow, ShopAverageLastColumn)), Type:=xlFillDefault Cells(ShopAverageLastRow + 1, 1) = "Sum of Hrs/pc" Cells(ShopAverageLastRow + 2, 1) = "#OP Completed" Cells(ShopAverageLastRow + 3, 1) = "Average Hrs/Pc" Range("B" & ShopAverageLastRow + 1) = "=SUM(B3:B" & ShopAverageLastRow & ")" Range("B" & ShopAverageLastRow + 2) = "=COUNTIFS('8MO Raw Data'!$D$2:$D$" & EightMOLastRow & ",B1,'8MO Raw Data'!$F$2:$F$" & EightMOLastRow & ",B2)" Range("B" & ShopAverageLastRow + 3) = "=B" & ShopAverageLastRow + 1 & "/B" & ShopAverageLastRow + 2 & "" Range(Cells(ShopAverageLastRow + 1, 2), Cells(ShopAverageLastRow + 3, 2)).Select Selection.AutoFill Destination:=Range(Cells(ShopAverageLastRow + 1, 2), Cells(ShopAverageLastRow + 3, ShopAverageLastColumn)), Type:=xlFillDefault 

一切 。 select ……

你可以尝试一个简单的事情就是把它放在开头

 Application.ScreenUpdating = False 

而当function完成时则相反

 Application.ScreenUpdating = True 

这将阻止Excel向您展示它在屏幕上所做的所有更改,这些更改可能占用很多时间。

你不应该真的需要这样做,因为你可以删除很多,如果不是所有的select语句。

 Range(Cells(3, 2), Cells(3, ShopAverageLastColumn)).Select Selection.AutoFill Destination:=Range(Cells(3, 2), Cells(ShopAverageLastRow, ShopAverageLastColumn)), Type:=xlFillDefault 

可以简化为以下,function相同。

 Range(Cells(3, 2), Cells(3, ShopAverageLastColumn)).AutoFill Destination:=Range(Cells(3, 2), Cells(ShopAverageLastRow, ShopAverageLastColumn)), Type:=xlFillDefault 

我想认为有些时候可以用这些build议中的一个或两个来保存。