Sub Test()
'PURPOSE: Determine how many seconds it took for code to completely run
'SOURCE: www.TheSpreadsheetGuru.com/the-code-vault
Dim StartTime As Double
Dim SecondsElapsed As Double
'Remember time when macro starts
StartTime = Timer
Dim cell As Range
Dim myfile As String
Dim wb As Workbook
Dim last As Long
Dim DestSh As Worksheet
shtname = ActiveSheet.Name
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
ActiveSheet.Range("A2", Range("B2").End(xlDown)).Copy
Set DestSh = Workbooks("BacktestFile.xlsm").Worksheets.Add
DestSh.Name = shtname & "T"
On Error Resume Next 'MIGHT BE HIDING AN ERROR CAUSING YOUR PROBLEM
ThisWorkbook.Sheets("Sheet1").Activate
ThisWorkbook.Sheets("Sheet1").Range("K1").PasteSpecial
ThisWorkbook.Sheets("Sheet1").Range("L1").Copy
ThisWorkbook.Sheets("Sheet1").Range("S11").PasteSpecial
ThisWorkbook.Sheets("Sheet1").Range("S12").Value = ThisWorkbook.Sheets("Sheet1").Range("S11").Value + 30
With ThisWorkbook.Worksheets("Sheet1")
For Each cell In .Range("K1:K1000") 'Column with the list of stocks
If cell.Value <> "" Then
cell.Copy
.Range("S5").PasteSpecial Paste:=xlPasteValues 'Cell S5 contains current stock symbol
Call GetYahooDataFromJSON 'This macros download historical price data
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DisplayStatusBar = False
Application.DisplayAlerts = False
last = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
Range("A1:G" & last).Sort [A1], xlAscending, Header:=xlYes
DestSh.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Value = ThisWorkbook.Sheets("Sheet1").Range("S5").Value
Worksheets("Sheet1").Range("F1:F" & last).Copy
DestSh.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).PasteSpecial Transpose:=True
Application.CutCopyMode = False
Else
End If
Next cell
End With
Worksheets("Sheet1").Range("A1:A" & last).Copy
DestSh.Range("B1").PasteSpecial Transpose:=True
DestSh.Activate
DestSh.Columns.AutoFit
DestSh.Range("A1").Activate
Dim colNo, colStart, colFinish, colStep As Long
Dim rng2Insert As Range
colStep = 2
colStart = Application.Cells(1, 4).Column + 1
colFinish = (ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Column * 2) - colStart
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
For colNo = colStart To colFinish Step colStep
ActiveSheet.Cells(1, colNo).EntireColumn.Insert
'***New code inserted
ActiveSheet.Cells(1, colNo) = "Change%"
ActiveSheet.Columns(colNo).NumberFormat = "0.00%"
ActiveSheet.Cells(2, colNo) = "=(RC[-1]-RC[colNo-2])/RC[colNo-2]"
ActiveSheet.Cells(2, colNo).Columns(colNo).AutoFill
'***
Next
'***New code inserted
ActiveSheet.Cells(1, colNo) = "Change%"
ActiveSheet.Columns(colNo).NumberFormat = "0.00%"
'***
Dim colFinish2 As Integer, totalRows As Integer
colFinish2 = (ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Column)
totalRows = WorksheetFunction.CountA(ActiveSheet.Range("B:B"))
On Error Resume Next
'Application.ScreenUpdating = False
For colNo = 5 To colFinish + 2 Step 2
With ActiveSheet
For rowno = 2 To totalRows
.Cells(rowno, colNo) = (.Cells(rowno, colNo - 1) - .Cells(rowno, 3)) / .Cells(rowno, 3)
Next
End With
Next
Call AverageTotals
ThisWorkbook.Sheets("Sheet1").Range("K:L").Clear
ThisWorkbook.Sheets("Sheet1").Range("S11:S12").Clear
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.DisplayStatusBar = True
Application.DisplayAlerts = True
Application.CutCopyMode = False
Application.Calculation = xlCalculationAutomatic
'Determine how many seconds code took to run
SecondsElapsed = Round(Timer - StartTime, 2)
'Notify user in seconds
MsgBox "This code ran successfully in " & SecondsElapsed & " seconds", vbInformation
End Sub