【VBA】特定の名前のシートを高速に転記する方法
日々の業務で大量のデータを手作業で移動するのは大変です。この記事では、特定の名前のシートからデータを高速に抽出し、別のシートへと配列を使用して効率的に転記する方法を紹介します。
サンプルコード
VBA
Option Explicit
Sub TransferSheetData()
' データソースのワークシート
Dim wsSource As Worksheet
Set wsSource = ThisWorkbook.Sheets("データ")
' 目的地のワークシート
Dim wsDestination As Worksheet
Set wsDestination = ThisWorkbook.Sheets("転記先")
' データソースの最終行を取得
Dim lastRowSource As Long
lastRowSource = wsSource.Cells(wsSource.Rows.Count, "A").End(xlUp).Row
' 配列にデータを読み込む
Dim data() As Variant
data = wsSource.Range("A1:C" & lastRowSource)
' 画面更新停止
Application.ScreenUpdating = False
' データ転記先の最終行を取得
Dim lastRowDestination As Long
lastRowDestination = wsDestination.Cells(wsDestination.Rows.Count, "A").End(xlUp).Row + 1
' 配列からデータを書き込む
wsDestination.Range("A" & lastRowDestination).Resize(UBound(data, 1), UBound(data, 2)).Value = data
' 画面更新再開
Application.ScreenUpdating = True
End Subよくある質問
Q 元に戻せますか?
A.
VBAの実行結果は「元に戻す」が効きません。必ずバックアップを取ってから実行してください。
Q エラーが出たら?
A.
シート名や列番号が正しいか確認してください。