📘 Excel逆引き事典

【VBA】全てのシートを部分一致で転記する方法

手作業で大量のデータを部分一致で転記するのは大変です。この記事では、VBAを使って簡単に全シートから特定の文字列を持つセルを別のシートに転記する方法をお伝えします。

サンプルコード

VBA
Option Explicit
Sub PartialMatchCopy()
    Dim ws As Worksheet, targetWs As Worksheet
    Dim lastRow As Long, i As Long
    Set targetWs = ThisWorkbook.Sheets("TargetSheet")
    Application.ScreenUpdating = False '画面更新を停止

    For Each ws In ThisWorkbook.Worksheets
        If ws.Name <> targetWs.Name Then
            With ws.UsedRange
                lastRow = .Rows.Count + .Row - 1
                For i = .Row To lastRow
                    If InStr(.Cells(i, 1).Value, "部分一致文字列") > 0 Then '条件を満たす場合
                        targetWs.Cells(targetWs.Rows.Count, 1).End(xlUp).Offset(1, 0).Value = ws.Cells(i, 1).Value '転記処理
                    End If
                Next i
            End With
        End If
    Next ws
    Application.ScreenUpdating = True '画面更新を再開
End Sub

よくある質問

Q 元に戻せますか?

A.
VBAの実行結果は「元に戻す」が効きません。必ずバックアップを取ってから実行してください。

Q エラーが出たら?

A.
シート名や列番号が正しいか確認してください。