Skip to content

Instantly share code, notes, and snippets.

@vascoferreira25
Last active March 14, 2019 17:18
Show Gist options
  • Save vascoferreira25/b9428877449d3dd27db19a3f523647a5 to your computer and use it in GitHub Desktop.
Save vascoferreira25/b9428877449d3dd27db19a3f523647a5 to your computer and use it in GitHub Desktop.
Option Explicit
' Description: Outputs quarterly average for a range monthly data.
' "quarter" for quarter reports
' "year" for yearly reports
Sub GenerateReport(term As String)
Dim portfolioData As Range
Set portfolioData = Range("B2", Range("B2").End(xlToRight).End(xlDown))
Dim columnCount As Long
columnCount = portfolioData.Columns.Count
Dim rowCount As Long
rowCount = portfolioData.Rows.Count
Dim reportCell As Range
Set reportCell = Cells(2, columnCount + 2)
Dim startRow As Long
Dim steps As Long
If term = "quarter" Then
startRow = 3
steps = 3
ElseIf term = "year" Then
startRow = 12
steps = 12
End If
Dim currentRow As Long
Dim currentCol As Long
Dim currentColumn As Range
For currentCol = 1 To columnCount
For currentRow = startRow To rowCount + startRow Step steps
Set currentColumn = Range(portfolioData.Cells(currentRow - startRow + 1, currentCol), portfolioData.Cells(currentRow, currentCol))
reportCell.Cells(currentRow + 1, currentCol).Value = WorksheetFunction.Average(currentColumn)
Next currentRow
Next currentCol
End Sub
Sub Report()
' "quarter" or "year"
GenerateReport "year"
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment