データの分析、グラフ作成、および通信のためのツールを備えた Microsoft 表計算ソフトウェアのファミリ
> sheet2のE5のセルを集計表のB8へ、> sheet2のN5のセルを集計表C8へと転記していきたく、> 最終的にsheet2のAF62のセルが集計表のBA8に来ます。
つまり次のようなコードを実行できればよい、ということでしょうか。
Sub CopyValues()
'定数の宣言
Const DestinationSheetName As String = "集計表" '転記先ワークシートの名前
Const DestinationFirstRow As Long = 8 '転記先ワークシートの先頭のデータ行の行番号
Dim wbkBook As Workbook
Dim wsDestination As Worksheet
'このマクロが記述されているブックを参照
Set wbkBook = ThisWorkbook
'転記先ワークシートを参照
Set wsDestination = wbkBook.Worksheets(DestinationSheetName)
'転記先ワークシートの A 列から BA 列までの全てのデータ行の値/数式をクリア
With wsDestination
.Range(.Cells(DestinationFirstRow, "A"), _
.Cells(.Rows.Count, "BA")).ClearContents
End With
Dim wsSource As Worksheet
Dim lngDestinationRow As Long
'転記先の行番号の初期値として、先頭のデータ行の行番号を代入
lngDestinationRow = DestinationFirstRow
'ブック内の全てのワークシートを順次参照する
For Each wsSource In wbkBook.Worksheets
'参照中のワークシートの名前をイミディエイトウィンドウに出力する(デバッグ用)
Debug.Print wsSource.Name
'参照中のワークシートの名前によって分岐する
Select Case wsSource.Name
'転記先ワークシート
Case DestinationSheetName
'何もしない
'その他のワークシート
Case Else
'参照中のワークシートの各セルの値を、転記先ワークシートのデータ行の各列のセルに代入する
wsDestination.Cells(lngDestinationRow, "A").Value = wsSource.Range("E4").Value
wsDestination.Cells(lngDestinationRow, "B").Value = wsSource.Range("E5").Value
wsDestination.Cells(lngDestinationRow, "C").Value = wsSource.Range("N5").Value
' :
' :
'D 列から AZ 列 までのセルへの代入も同様に記述する
' :
' :
wsDestination.Cells(lngDestinationRow, "BA").Value = wsSource.Range("AF62").Value
'転記先の行番号を1進める
lngDestinationRow = lngDestinationRow + 1
End Select
Next
Set wsDestination = Nothing
Set wbkBook = Nothing
End Sub