シート間貼り付け
VBAでシート間をまたいで貼り付けるコードを記述しました。
後で編集しやすいように、宣言しているため少し冗長ですが…
宣言まとめて同じ型でできる方法なかったっけ・・・?
後日考え方などについて記述しようと思います。
Option Explicit Sub Household() Dim houseHoldWs As Worksheet: Dim nowHouseholdWs As Worksheet Dim tSRow As LongPtr: Dim tSCol As LongPtr: Dim tERow As LongPtr Dim targetRow As Long: Dim targetSCol As LongPtr: Dim targetECol As LongPtr Dim cumSRow As LongPtr: Dim cumSCol As LongPtr: Dim cumERow As LongPtr: Dim cumECol As LongPtr Dim cumagoSRow As LongPtr: Dim cumagoSCol As LongPtr: Dim cumagoERow As LongPtr: Dim cumagoECol As LongPtr Dim targetMCol As LongPtr: Dim targetMonth As String Dim rc As Integer: Dim i As LongPtr 'シートセット Set houseHoldWs = ThisWorkbook.Worksheets("家計簿") Set nowHouseholdWs = ThisWorkbook.Worksheets("家計簿当月表示") '初期値設定 tSRow = 3: tSCol = 3: tERow = 10: cumSRow = 3: cumSCol = 6: cumERow = 10: cumECol = 7 cumagoSRow = 3: cumagoSCol = 10: cumagoERow = 10: cumagoECol = 11 targetSCol = 2: targetECol = 9: targetMCol = 1 '転記対象の行取得 targetRow = ActiveCell.Row targetMonth = houseHoldWs.Cells(targetRow, targetMCol).Value rc = MsgBox("転記対象は" & targetMonth & "です。", vbOKCancel) '処理中断 If rc = 2 Then: Exit Sub: End If '前月累計転記 nowHouseholdWs.Range(nowHouseholdWs.Cells(cumagoSRow, cumagoSCol), nowHouseholdWs.Cells(cumagoERow, cumagoECol)).Value = _ nowHouseholdWs.Range(nowHouseholdWs.Cells(cumSRow, cumSCol), nowHouseholdWs.Cells(cumERow, cumECol)).Value '当月家計簿転記 For i = targetSCol To targetECol nowHouseholdWs.Cells(i + 1, tSCol).Value = houseHoldWs.Cells(targetRow, i).Value Next i End Sub