Sub Merge_and_Chart()
Application.ScreenUpdating = False
Dim i As Long, j As Long, mylastrow As Long
Dim rng1 As Range, rng2 As Range, rng3 As Range, rng4 As Range, rng5 As Range
Dim myval As String, lastcolumn As Long
Dim add1 As Long, add2 As Long
Dim xrow As Long, lastrow As Long, rng As Range
For i = 1 To Worksheets.Count - 1
Sheets(i).Activate
With Sheets(i)
Set rng1 = Sheets(i).Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row)
Set rng2 = Sheets(i).Range("B2:B" & Cells(Rows.Count, 2).End(xlUp).Row)
Set rng3 = Sheets(i).Range("C2:C" & Cells(Rows.Count, 3).End(xlUp).Row)
Set rng5 = Sheets(i).Range("E2:E" & Cells(Rows.Count, 5).End(xlUp).Row)
Set rng4 = Sheets(i).Range("F2:F" & Cells(Rows.Count, 6).End(xlUp).Row)
End With
Sheets("Merged").Activate
Sheets("Merged").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Resize(rng1.Rows.Count, rng1.Columns.Count).Cells.Value = rng1.Cells.Value
Sheets("Merged").Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).Resize(rng2.Rows.Count, rng2.Columns.Count).Cells.Value = rng2.Cells.Value
Sheets("Merged").Cells(Rows.Count, 3).End(xlUp).Offset(1, 0).Resize(rng3.Rows.Count, rng3.Columns.Count).Cells.Value = rng3.Cells.Value
Sheets("Merged").Cells(Rows.Count, 4).End(xlUp).Offset(1, 0).Resize(rng4.Rows.Count, rng4.Columns.Count).Cells.Value = rng4.Cells.Value
Sheets("Merged").Cells(Rows.Count, 5).End(xlUp).Offset(1, 0).Resize(rng5.Rows.Count, rng5.Columns.Count).Cells.Value = rng5.Cells.Value
Next
Range("D:D").Copy Range("G1")
Range("G2").Activate
Range(Selection, Selection.End(xlDown)).Select
Selection.RemoveDuplicates Columns:=Array(1), Header:=xlNo
Selection.Sort key1:=ActiveCell, order1:=xlAscending
Range("H1").Value = "Year"
Range("I1").Value = "Amount"
Range("F1").Activate
mylastrow = Cells(Rows.Count, 7).End(xlUp).Row
For j = 2 To mylastrow
myval = Cells(j, 7).Value
lastrow = Cells(Rows.Count, 1).End(xlUp).Row
lastcolumn = _
Cells.Find(What:="*", After:=Range("A1"), SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Offset(0, -4).Column
add1 = Columns(4).Find(What:=myval, LookIn:=xlValues, LookAt:=xlWhole).Row
xrow = add1
Do
If Cells(xrow + 1, 4).Value <> myval Then
add2 = xrow
Exit Do
Else
xrow = xrow + 1
End If
Loop Until xrow = lastrow + 1
Range(Cells(add1, lastcolumn), Cells(add2, lastcolumn)).Copy Cells(Rows.Count, 9).End(xlUp).Offset(1, 0)
Range(Cells(add1, 4), Cells(add1, 4)).Copy Cells(add1, 8)
Next
Range("H1:I" & Cells(Rows.Count, 9).End(xlUp).Row).Select
Set rng = Selection
ActiveSheet.Shapes.AddChart2(332, xlLineMarkers).Select
ActiveChart.SetSourceData Source:=rng
ActiveSheet.Shapes("Chart 1").IncrementLeft 38.25
ActiveSheet.Shapes("Chart 1").IncrementTop -166.5
ActiveSheet.Shapes("Chart 1").ScaleWidth 1.1979166667, msoFalse, _
msoScaleFromTopLeft
Range("F1").Select
Application.ScreenUpdating = True
End Sub