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
これは非常に高速であるべきだと思います。Destination 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
お時間とご回答ありがとうございます。