我试图自动化具有 5 个不同信息源的报告。我试图使用 ListObjects 将不同表的 UNION 合并为一个,除了复制第一个 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
提前感谢您的时间和答案