【VBA】特定の名前のシートをバックグラウンドで転記する方法
手作業でのデータ移動は時間がかかるだけでなく、ミスも起こりやすいです。この記事では、特定の名前のシートを別の場所に自動でバックグラウンドで転記する方法を紹介します。
サンプルコード
VBA
Option Explicit
Sub CopySheetInBackground()
Dim ws As Worksheet, targetWs As Worksheet
Dim lastRow As Long, i As Long
' バックグラウンド処理の開始
Application.ScreenUpdating = False
Set ws = ThisWorkbook.Sheets("転記元")
If ws Is Nothing Then Exit Sub
' 転記先シートの作成または取得
On Error Resume Next
Set targetWs = ThisWorkbook.Sheets("転記先")
If targetWs Is Nothing Then
Set targetWs = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
targetWs.Name = "転記先"
End If
On Error GoTo 0
' データのコピー
lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
For i = 1 To lastRow
targetWs.Cells(i, 1).Value = ws.Cells(i, 1).Value
Next i
' バックグラウンド処理の終了
Application.ScreenUpdating = True
End Subよくある質問
Q 元に戻せますか?
A.
VBAの実行結果は「元に戻す」が効きません。必ずバックアップを取ってから実行してください。
Q エラーが出たら?
A.
シート名や列番号が正しいか確認してください。