|
副作用のないバージョン
Sub Macro3()
Const FirstRow = 1
Const SourceCol = 1
Const TargetRow = 1
Const FirstCol = 4
Dim SourceRow As Long
Dim TargetCol As Long
Dim FoundCell As Range
Dim SearchCol As Long
TargetCol = FirstCol - 2
For SourceRow = FirstRow To Cells(Rows.Count, SourceCol).End(xlUp).Row
' 地味に探すバージョン
Set FoundCell = Nothing
For SearchCol = FirstCol To TargetCol Step 2
If Cells(TargetRow, SearchCol).Value = Cells(SourceRow, SourceCol).Value Then
Set FoundCell = Cells(TargetRow, SearchCol + 1)
Exit For
End If
Next SearchCol
If FoundCell Is Nothing Then
TargetCol = TargetCol + 2
' 2値代入バージョン
Cells(TargetRow, TargetCol).Resize(1, 2).Value = Cells(SourceRow, SourceCol).Resize(1, 2).Value
Else
' 値加算バージョン
FoundCell.Value = FoundCell.Value + Cells(SourceRow, SourceCol + 1).Value
End If
Next SourceRow
End Sub
|
|