CurrentRegion.SpecialCells(xlCellTypeVisible)太慢 – 提高性能的提示?

我试图自动化有5个不同的信息来源的报告。 我尝试使用ListObjects将不同的表组合成一个单独的表,除非当我复制第一个ListObject的第一列时,一切正常。 复制第一列大约需要2分钟,接下来的列不到1秒。

每次运行VBA脚本时,我都会删除目标表中的所有行,以便使用带有0行的ListObject来启动VBA脚本。

我会试着解释它是如何工作的:

Sub ProcesarPresupuesto() 'This is the first macro that process and copy the information of the first source Application.ScreenUpdating = False Application.DisplayAlerts = False Application.Calculation = xlCalculationManual '<Here> I add several columns and process the information of this first source, I keep all the rows as values using the Function: AddColumnFormula (at the end of this example). I think this is not causing the problem. 'Then I fill all the Blanks Cells to avoid having empty cells in my final table. Sheets("Origin").Select Selection.CurrentRegion.Select On Error Resume Next Selection.SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "Null" On Error GoTo 0 'When I have the ListObject ready I start copying the columns to the destination Sheets("Destination").Select Range("A1").Select While ActiveCell.Value <> "" Call CopyColumn("Origin", ActiveCell.Value, "Destination") ActiveCell.Offset(0, 1).Select Wend End Sub 

我认为这应该是非常快的。 如果我只删除目标ListObject的值,并保持行为空,第一列立即复制,所以我认为这个问题是关于如何Excel计算要添加到ListObject的第一行。 有没有更好的方式来复制列时,表是空的? 我做错了什么吗?

这是函数CopyColumn

 Function CopyColumn(Origin, ColumnName, Destination) Range(Origin & "[[" & ColumnName & "]]").Copy Destination:=Range(Destination & "[[" & ColumnName & "]]") End Function 

这是我用来处理列的函数

 Function AddColumnFormula(DestinationSheet, TableName, ColumnName, Value) Set NewColumn = Sheets(DestinationSheet).ListObjects(TableName).ListColumns.Add NewColumn.Name = ColumnName Set Rango = Range(TableName & "[[" & ColumnName & "]]") Rango.Value = Value Rango.Copy Rango.PasteSpecial (xlPasteValues) End Function 

预先感谢您的时间和答案

我用你提供的文件做了一些testing。 这很慢,但我一开始没有时间。 我看到了一些修改代码的机会, 可以提高性能,计时器花了1分16秒。

我尝试了几个不同的成功,使用Debug.Print语句告诉我代码运行的是什么部分,以及他们要花多长时间。 大多数处决都是大约2分钟,最慢的是3分13秒。

在最后的3个月的尝试中,我把重点放在了:

...CurrentRegion.SpecialCells(xlCellTypeBlanks)

这是可疑的,因为CurrentRegionSpecialCells方法都可能是昂贵的。 把它们结合起来就像是一场灾难。

我想我会尝试一个简单的迭代,只是为了比较性能,而令我惊讶的是,我能够做一个简单的For each循环超过42000行和32列的数据,这将执行在大约14秒运行时间约30秒。

这是我用于循环的代码:

 Dim cl As Range 'Debug.Print "For each ..." & Format(Now(), "hh:mm:ss") For Each cl In wsP.ListObjects(1).DataBodyRange If cl.Value = vbNullString Then cl.Value = "Null" Next 'Debug.Print "End loop " & Format(Now(), "hh:mm:ss") 

这是我最后的三个结果:

 31 seconds: Commencar a 21:09:25 For each ...21:09:38 End loop 21:09:52 CopiarColumnaListOBjectaVacia...21:09:52 Finito : 5/5/2014 9:09:56 PM 30 seconds: Commencar a 21:10:23 For each ...21:10:36 End loop 21:10:49 CopiarColumnaListOBjectaVacia...21:10:49 Finito : 5/5/2014 9:10:53 PM 34 seconds: Commencar a 21:18:42 For each ...21:18:55 End loop 21:19:09 CopiarColumna... 21:19:09 Finito : 5/5/2014 9:19:16 PM 

我已经在Google文档中保存了XLSB的修订版本,以便您可以完整地查看。

https://drive.google.com/file/d/0B1v0s8ldwHRYZWhuTmRuaDJoMzQ/edit?usp=sharing

正如我所说的,我对这个子程序和RenombraColumna做了一些修改,但事后看来,这些可能会提供一些效率,我认为问题的根源在于CurrentRegion.SpecialCells

我希望你们不要介意我把这个问题的标题修改得更适合特定的问题。 正如原来所说,这个问题不太可能帮助其他人有相同的症状。