Option Explicit
'Code created and edited by NBVC and Ska67Can
Sub CopyPaste()
Dim sht As Worksheet
Dim LastRow As Long, LastCol As Long, LastRow2 As Long, LastCol2 As Long
For Each sht In ActiveWorkbook.Sheets
If sht.Name <> "Instructions" And sht.Name <> "SATcosts" Then
LastRow = sht.Cells(65536, 4).End(xlUp).Row
LastCol = sht.Cells(5, 256).End(xlToLeft).Column
' Check if Year has changed. If yes, proceed, else warning given.
If sht.Cells(1, 2).Value <> Year(Now()) Then
sht.Cells(4, LastCol + 1).Value = sht.Cells(1, 2).Value - 1
Range(sht.Cells(5, 4), sht.Cells(LastRow, 5)).Copy
sht.Cells(5, LastCol + 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'sht.Cells(5, LastCol + 1).Value = 0
LastCol2 = sht.Cells(5, 256).End(xlToLeft).Column
LastRow2 = sht.Cells(65536, LastCol2).End(xlUp).Row
' calculate percentage change
With sht.Range(Cells(5, LastCol2 + 1).Address, Cells(LastRow2, LastCol2 + 1).Address)
.Formula = "=(" & Cells(6, LastCol2).Address(rowabsolute:=False) & "-" & Cells(6, LastCol2 - 2).Address(rowabsolute:=False) & ")/" & Cells(6, LastCol2).Address(rowabsolute:=False)
End With
With Range(Cells(5, LastCol2 + 1), Cells(LastRow2, LastCol2 + 1))
.Font.ColorIndex = 41
End With
' Add left/right borders
With Range(sht.Cells(4, LastCol2 - 1), sht.Cells(LastRow2, LastCol2)).Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Range(sht.Cells(4, LastCol2 + 1), sht.Cells(LastRow2, LastCol2)).Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Range(sht.Cells(4, LastCol2 - 1), sht.Cells(4, LastCol2 + 1)).Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Range(sht.Cells(4, LastCol2 - 1), sht.Cells(4, LastCol2 + 1)).Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
Application.CutCopyMode = False ' Clear clipboard so big clipboard dialog will not appear when closing main workbook
Else
MsgBox "Year has not changed!"
Exit For
End If
End If
Next sht
End Sub
Bookmarks