I would be grateful for any help with workbook closing itself in the middle of macros run-time. The macros downloads list of stocks historical price data and inserts them in the corresponding to each stock CSV file in the folder C:\VBA\. Names of CSV files are equal to their corresponding stock symbols. If stock symbol is XYZ then its file will be XYZ.csv. Whenever corresponding to stock symbol CSV file can’t be found in the folder with CSV files, workbook closes itself in the middle of run-time. I can't figure out how to solve this problem. Thanks in advance for any help.
Dim cell As Range Dim myfile As String Dim directory As String Dim FileExt As String Application.ScreenUpdating = False Application.EnableEvents = False Application.DisplayStatusBar = False Application.DisplayAlerts = False On Error Resume Next For Each cell In Range("K1:K100") 'Column with the list of stocks If cell.Value <> "" Then Worksheets("Sheet1").Range("S5").Value = cell.Value 'Cell S5 contains current stock symbol Call GetYahooDataFromJSON 'This macros download historical price data directory = "C:\VBA\" 'Directory where all corresponding to stocks CSV files stored FileExt = ".csv" 'File extensions 'Cell S5 = value = stock symbol myfile = directory & Cells(5, 19).Value & FileExt 'Path to the current CSV file to be opened Application.Workbooks.Open Filename:=myfile ActiveSheet.Range("B2").Insert Shift:=xlShiftDown 'Destination range in the opened CSV file ActiveWorkbook.Close True 'This line causes troubles if corresponding to stock CSV file does not exist in the folder, it closes workbook with macros. End If Next cell Application.ScreenUpdating = True Application.EnableEvents = True Application.DisplayStatusBar = True Application.DisplayAlerts = True Application.CutCopyMode = False End Sub