Excel + VBA To Create A Unique File For 500 Stocks

The purpose of this is to demonstrate the power of VBA with formatting a file, creating a pivot table, and saving files as PDFs. The PDF portion is something I’ll be exploring over a series of posts, but long story short – VBA is more powerful than Google Apps Script for creating PDFs. In addition, it is easier to create a PDF template in Excel vs python.

We have Open, High, Low, Close (OHLC) data from Yahoo Finance for all of the tickers in the S&P, which looks like this

Formatting The Initial Data By Running The Following Code

Sub FormatOHLCData()
    ' Declare variables
    Dim ws As Worksheet
    Dim tbl As ListObject

    ' Set the worksheet
    Set ws = ThisWorkbook.Sheets("OHLC")

    ' Convert the data range to a table
    Set tbl = ws.ListObjects.Add(xlSrcRange, ws.UsedRange, , xlYes)
    tbl.Name = "OHLC_Table"

    ' Bold headers
    tbl.HeaderRowRange.Font.Bold = True

    ' Freeze top row of the entire worksheet
    ws.Rows(2).Select
    ActiveWindow.FreezePanes = True

    ' Format Date column to MM/DD/YYYY format
    tbl.ListColumns("Date").DataBodyRange.NumberFormat = "mm/dd/yyyy"

    ' Format OHLC data to number format with 2 decimals
    tbl.ListColumns("Open").DataBodyRange.NumberFormat = "0.00"
    tbl.ListColumns("High").DataBodyRange.NumberFormat = "0.00"
    tbl.ListColumns("Low").DataBodyRange.NumberFormat = "0.00"
    tbl.ListColumns("Close").DataBodyRange.NumberFormat = "0.00"

    ' Center everything on the page
    ws.Cells.HorizontalAlignment = xlCenter
    ws.Cells.VerticalAlignment = xlCenter
    
    ws.Columns.AutoFit
    ws.Range("A1").Select
    
End Sub

Creating A Pivot Table (code not shown & can be created manually)

Creating A Unique File For Each Ticker Symbol & Saving In A Standardized Manner

  • Goal is to loop through each symbol and then save that tab as a PDF and then repeat
  • Speed: it took 78 seconds to create 500 PDFs by looping through a pivot table
Sub LoopPivotTable_Save()

Dim pt As PivotTable, pi As PivotItem, pf As PivotField
Dim Val As String
Dim filePath As String
Dim todayDate As String
Dim folderPath As String
    
Application.ScreenUpdating = False
Application.DisplayAlerts = False

Sheets("pvtTable").Select

Set pt = ActiveSheet.PivotTables(1)
Set pf = pt.PageFields(1)
    
todayDate = Format(Date, "YYYYMMDD")
folderPath = ThisWorkbook.Path & "\" & todayDate
    
' Get today's date in the format YYYYMMDD
todayDate = Format(Date, "YYYYMMDD")

' Set the folder path
folderPath = ThisWorkbook.Path & "\" & todayDate

' Create a new folder with today's date
MkDir folderPath


'Loops through a pivot table and sets name of of sheet to the name of the Symbol
For Each pi In pf.PivotItems
  'Debug.Print pi
  pf.CurrentPage = pi.Value
  Val = ActiveSheet.Range("B1").Value
  ActiveSheet.Name = Val
  
  'fpath
  filePath = folderPath & "\" & pi & " 1 OHLC 1 Pager.pdf"

  'Save PDF
  ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=filePath, Quality:=xlQualityStandard

Next pi

'Set name back to pvtTable
ActiveSheet.Name = "pvtTable"

Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub

Sharing

Related Articles

  • All Post
  • Articles
  • Blog Post
  • General Business Automation
  • Portfolio
  • Stock Market & Finance